leetcode刷题,总结,笔记,备忘。136

从通过率最高的题目开始136 Single Number

找出数组里唯一一个只出现一次的数字。通过这个题目知道位异或运算符连续异或2个相同的数字得到0。

/*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?
 */
# include <stdio.h>

int singleNumber(int* nums, int numsSize) 
{
    int i;
    int sum = 0;
    
    for (i=0; i<numsSize; i++)
    {
        sum ^= nums[i];
    }
    
    return sum;
}

int main(void)
{
	int array[] = {1, 2, 2, 3, 4, 3, 1};
	int sum;

	sum = singleNumber(array, sizeof(array) / sizeof(array[0]));
	printf("%d\n", sum);

	return 0;
}

你可能感兴趣的:(LeetCode,Algorithm)