LeetCode--Single Numberコード分析


LeetCodeで提出された2番目の問題は、Cの「ビット演算」の一節だと言うべきだが、誰も考えられない.
位异或(^)演算子によってこのように试験することができて、确かにとても感心して问题を出す人の思惟と知能指数、あまり言わないで、直接コードを见ましょう;
コード実装:
#include <iostream> #include <fstream> using namespace std; /*      :   C/C++   ,       ^                  ,             ,      1,     :  (1) 1 ^ 2      : 0000 0001          ^ 0000 0010          ____________          = 0000 0011 = 3(   )  (2) 1 ^ 1             0000 0001           ^ 0000 0001           ___________           = 0000 0000 = 0(   )                  0, 0 ^ a == a,    ,             ,                . */ class Solution { public:     int singleNumber(int A[], int n)     {         // IMPORTANT: Please reset any member data you declared, as         // the same Solution instance will be reused for each test case.         if (n <= 0)             return -1;         int ans = 0;         for (int i = 0; i < n; i++)             ans ^= A[i];         return ans;     } }; //      int main() {     int a[7];      //freopen("input.txt","r",stdin);      Solution s;      for(int i = 0; i < 7; i++)         cin >> a[i];     cout << s.singleNumber(a,7) << endl;     return 0; } //    : //1 2 3 1 2 3 4