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;
}
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;
}
相关文章推荐
- 【leetcode】【Single Number题目】java 异或运算解决数字出现偶数次还是奇数次问题||HashMap
- [leetcode]单链表类题目总结(应用双指针)
- 异或操作的应用
- 前缀树及应用两数异或Leetcode之 Maximum XOR of Two Numbers in an Array
- 浅谈异或运算符的应用及相关题目题解
- hdu 4768 Flyer (异或操作的应用)
- leetcode 268 Missing Number(异或运算的应用)
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
- 【算法——Python实现】快速排序的优化:三路快速排序及Leetcode题目应用
- 关于C语言字符串操作的几个题目
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
- 几个字符串相关的题目,来自LeetCode和LintCode
- leetcode-371. Sum of Two Integers 位运算的应用,递归,按位异或a^b
- 异或解决LeetCode两道算法题目
- leetcode -- Text Justification -- string操作的题目,思路简单,但是难以写对
- LeetCode 136:Single Number(异或操作)
- LeetCode Online Judge 题目C# 练习 - Simplify Path
- C++ Builder几个应用技巧
- 使用SVN和VS2013新建web应用项目工程简易操作流程
- 开发大型高负载类网站应用的几个要点