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

这题目的要求不仅是要求是线性时间,希望也不会使用额外的内存,那么也就是你无法运用其他的数据结构。也是参考了其他人的答案。对于位操作的特性还有待进一步挖掘。

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

 

posted on 2016-08-31 04:59  医生工程师  阅读(136)  评论(0编辑  收藏  举报

导航