码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode[136]Single Number

时间:2015-05-17 18:26:20      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

/* 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 }

 

LeetCode[136]Single Number

标签:

原文地址:http://www.cnblogs.com/shiddong/p/4510083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!