题目136:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
/* 对数组全部元素进行异或操作,相同元素异或结果为0,结果即为单个的数 */
int singleNumber(int* nums, int numsSize)
{
int res = nums[0];
int i = 0;
for(i = 1; i < numsSize; i++)
{
res ^= nums[i];
}
return res;
}
本文介绍了一种高效的方法来解决数组中找到唯一非重复元素的问题,通过使用异或运算实现线性时间复杂度且不需要额外内存。
608

被折叠的 条评论
为什么被折叠?



