异或操作:
主要是知道异或操作符是 ^ ,就好
class Solution {
public int xorOperation(int n, int start) {
int[] nums = new int[n];
int result = 0;
for(int i = 0; i<n; i++){
nums[i]=start + 2*i;
result = result ^ nums[i];
}
return result;
}
}
汉明距离:Integer.bitCount
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
只出现一次的数字:
奇妙之处:数组中的全部元素的异或运算结果即为数组中只出现一次的数字
class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for(int num : nums){
single ^= num;
}
return single;
}
}