Gray Code (格雷码编码) 【leetcode】
来源:互联网 发布:java人民币大小写转换 编辑:程序博客网 时间:2024/05/14 06:13
题目:
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]
. Its gray code sequence is:
00 - 001 - 111 - 310 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1]
is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
看到坑爹的特判表示放弃,查了一下格雷码的生成,果然知识就是力量。
二进制转格雷码:
只要异或乘以二分之三,即二进制的1.1,然后忽略小数部分;也可以理解成异或乘以三(即11),再右移一位。
格雷码转二进制:
异或乘以三分之二,即除以1.1,忽略余数;或者左移一位,再异或除以三,忽略余数。
class Solution{public:vector<int> grayCode(int n){vector<int> ret;int maxx = 1 << n;for(int i = 0; i < maxx; ++i)ret.push_back((i >> 1)^i);return ret;}};
- Gray Code (格雷码编码) 【leetcode】
- LeetCode | Gray Code(格雷码)
- Gray Code 格雷码编码
- [LeetCode] 格雷码(Gray Code)
- Gray Code 格雷码 @LeetCode
- leetcode Gray Code 格雷码
- LeetCode-Gray Code(格雷码)
- LeetCode OJ 之 Gray Code(格雷码)
- [C++]LeetCode: 86 Gray Code (格雷码)
- LeetCode 89. Gray Code(生成格雷码)
- LeetCode 89. Gray Code(格雷码)
- LeetCode#89. Gray Code(格雷码)
- LeetCode 89: Gray Code 格雷码
- LeetCode(87) Gray Code
- Gray Code(Leetcode 89)
- 格雷码(Gray Code)
- leetCode 89.Gray Code (格雷码) 解题思路和方法
- [LeetCode]—Gray Code 计算格雷码
- c语言define的用法
- cocos2d-x-2.1.3 中实现游戏的简单暂停与恢复
- Android如何下载服务器文件
- RTMPDump源代码分析 0: 主要函数调用分析
- Queen 八皇后问题 回溯经典解法
- Gray Code (格雷码编码) 【leetcode】
- HDU 2161 Primes(素数打表)
- IO流之File类
- hdu 一卡通大冒险 递推 或是 dp
- 韦东山视频实验之USB鼠标驱动—OOPS错误分析
- 浅谈一下大数相乘有关思路(图解)与用java代码具体解决方案
- delphi Format格式化函数
- 计算机开机奥秘
- [西山居笔试]分别写一个宏和函数来获取元素个数 如count(a) 会得到a数组元素个数