题目
思路
- 交换律:a ^ b ^ c <=> a ^ c ^ b
- 任何数于0异或为任何数 0 ^ n => n
- 相同的数异或为0: n ^ n => 0
代码
只出现一次的数字public int singleNumber(int[] nums) {if (nums.length == 0) return 0;int res = nums[0];for (int i = 1; i < nums.length; i++) {res = res ^ nums[i];}return res;}
