LeetCode HOT100 - 只出现一次的数字

一个数和其自身的异或和是 0

因此对数组中的所有元素进行异或,剩下的那个数就是只出现了一次的数

class Solution {
public:
    int singleNumber(vector<int>& a) {
        int sum = 0;
        int n = a.size();
        for (int i = 0; i < n; i++) {
            sum ^= a[i];
        }
        return sum;
    }
};
posted @ 2026-03-22 21:08  rdcamelot  阅读(1)  评论(0)    收藏  举报