落单的数

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。

样例

给出 [1,2,2,1,3,4,3],返回 4

挑战

一次遍历,常数级的额外空间复杂度

标签

贪心

code

class Solution {
public:
	/**
	 * @param A: Array of integers.
	 * return: The single number.
	 */
    int singleNumber(vector<int> &A) {
        // write your code here
        int ans = 0;
        for(int i=0; i<A.size(); ++i)
            ans ^= A[i];
        return ans;
    }
};

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2021-11-10
  • 2022-02-13
  • 2022-12-23
猜你喜欢
  • 2021-07-12
  • 2021-09-03
  • 2022-12-23
  • 2021-12-02
  • 2021-10-18
  • 2021-06-19
  • 2021-06-10
相关资源
相似解决方案