您的位置:首页 > 其它

LeetCode中应用异或操作的几个题目

2017-08-07 13:02 253 查看
1.LeetCode 89题 Gray Code

public List<Integer> grayCode2(int n) {
List<Integer> result = new LinkedList<>();
for (int i = 0; i < 1 << n; i++){
result.add(i ^ i >> 1);
}
return result;
}

2.LeetCode 136题 Single Number
public int singleNumber(int[] nums) {
int result = 0;
if(nums.length ==1){
return nums[0];
}else{
for(int i = 0; i < nums.length; i++){
result ^= nums[i];
}
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: