LeetCode刷题:136.Single Number


136. Single Number

Given a non-empty 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?
Example 1:

Input: [2,2,1]

Output: `1

Example 2:

Input: [4,1,2,1,2]

Output: 4

解题思路:

题目要求算法的时间复杂度为O(n),空间复杂度为O(1);
将容器的所有的数字进行异或运算,由于相同两个数字异或结果为0,所以所有元素异或的结果便为所寻单独的数字。

解答:
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int result = 0;
        for(auto ibeg = nums.begin(); ibeg != nums.end(); ++ibeg)
        {
            result ^= *ibeg;  //所有数字进行异或运算 
        }
        return result;
    }
};

文章作者: Jason
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jason !
  目录