[LeetCode]Gray Code
来源:互联网 发布:杭州 夜场小姐数据 编辑:程序博客网 时间:2024/06/10 20:52
题目:给定一个数字n,表示二进制数字的位数,求出n位格雷码对应的十进制数
例如,给定n=2,则返回 [0,1,3,2]
. 它的格雷码序列是:
00 - 001 - 111 - 310 - 2算法:
二进制二进制右移格雷码--------------------------000000000001000001010001011011001010100010110101010110110011101111011100 得出 : garyCode = x ^ (x>>1)
public class Solution {/** * Algorithm: * * binarybinary>>1gray * -------------------------- * 000000000 * 001000001 * 010001011 * 011001010 * 100010110 * 101010110 * 110011101 * 111011100 * * find : garyCode = x ^ (x>>1) * */ public List<Integer> grayCode(int n) { List<Integer> grayCodeList = new ArrayList<Integer>(); int nLoops = (1 << n); // 2^n-1 for (int i=0; i<nLoops; ++i) { int grayCode = (i ^ (i >> 1)); grayCodeList.add(grayCode); } return grayCodeList; }}
2 0
- LeetCode: Gray Code
- [LeetCode] 格雷码(Gray Code)
- LeetCode Gray Code
- [Leetcode] Gray Code
- [LeetCode] Gray Code
- leetcode 112: Gray Code
- LeetCode Gray Code
- [leetCode] Gray Code
- [LeetCode]Gray Code
- Leetcode: Gray Code
- [leetcode]Gray Code
- LeetCode-Gray Code
- [leetcode] Gray Code
- LeetCode - Gray Code
- leetcode之Gray code
- LeetCode:Gray Code
- 【转载】【leetcode】Gray Code
- LeetCode - Gray Code
- Android系统隐藏系统状态栏
- UVa 11100 The Trip, 2007 解题报告(策略)
- GNU开发环境基础,gcc,gdb,makefile, init启动过程
- HUDJ 1257 最少拦截系统 贪心
- Supervisor + Tornado 之吐槽
- [LeetCode]Gray Code
- lua调试打印table算法
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
- 使用CSVDE命令批量导入/导出AD用户
- 回收站文件清理了还能找回来吗
- ZOJ 2334 HDU 1512 Monkey King
- QUI框架V3.3版本正式发布!
- linux学习笔记
- Android PullToRefresh 下拉刷新,上拉更多,支持ScrollView,ListView,可方便拓展GridView,WebView等