136. 只出现一次的数字
题目

题解
- 考察的是位运算 —— 异或(^),相同为 0,不同为 1
 - 1^0 = 1,1^1 = 0
 - 则直接对数据所有元素执行 ^ 操作,最终的就是结果
 
class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
        for (int num : nums) {
            res = res ^ num;
        }
        return res;
    }
}
本文由mdnice多平台发布

                
            
        
浙公网安备 33010602011771号