首页 > 其他 > 详细

Leetcode-136 Single Number

时间:2016-11-27 22:26:53      阅读:268      评论:0      收藏:0      [点我收藏+]

#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?

题解:既然要求线性时间复杂度求解这题,空间复杂度为O(1).就不能暴力轮询啦。

然后就想起数电里面,XOR异或,相同的数异或是零。老实说,为啥我看到single想到了单身狗,两个配对的数一异或就归零哈哈。

然后就剩下了单身狗数。

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int single=0;
        for(int i=0;i<nums.size();i++)
        {
            single=single^nums[i];
        }
        return single;
    }
};

 

      

Leetcode-136 Single Number

原文:http://www.cnblogs.com/fengxw/p/6107000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!