标签:
/* LeetCode[136]:Single Number
* 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,所以将所有数字都异或后剩下的就是出现一次的数
1 int singleNumber(int *nums, int numsSize) 2 { 3 int single = 0; 4 int i = 0; 5 for(i=0; i<numsSize; i++) 6 { 7 single^=nums[i]; 8 } 9 return single; 10 }
标签:
原文地址:http://www.cnblogs.com/shiddong/p/4510083.html