编程之美[leetcode_136]Single Number输入一个数组,里面每个元素都有两个,但是有一个元素只有一个。请在不消耗额外内存空间的情况下,找出那个单一的元素并输出。此题用异或即可。不会消耗额外的内存空间。一次AC,附上代码: 1 2 3 4 5 6 7 8 9 10 11 12 class Solution { public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int ans = A[0]; for(int i = 1; i < n; i++) { ans = ans ^ A[i]; } return ans; } };