89. Gray Code #Medium
2016-06-08 10:46
441 查看
leetcode 89. Gray Code #Medium
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 - 0
01 - 1
11 - 3
10 - 2
题意:
根据所给数字n生成2^n个连续的格雷码
分析:
举例
n=1, [0, 1]
n=2, [00, 01, 11, 10]
n=3, [000, 001, 011, 010, 110, 111, 101, 100]
后一个序列的前半段就是前一个序列,后半段是前一个序列的每个数字左边补1,然后反向排列这些数字。根据此规律可求解
C++代码:
[code]class Solution {
public:
vector grayCode(int n) {
vector r1(1,0),r2;
if(n
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 - 0
01 - 1
11 - 3
10 - 2
题意:
根据所给数字n生成2^n个连续的格雷码
分析:
举例
n=1, [0, 1]
n=2, [00, 01, 11, 10]
n=3, [000, 001, 011, 010, 110, 111, 101, 100]
后一个序列的前半段就是前一个序列,后半段是前一个序列的每个数字左边补1,然后反向排列这些数字。根据此规律可求解
C++代码:
[code]class Solution {
public:
vector grayCode(int n) {
vector r1(1,0),r2;
if(n
相关文章推荐
- SSH Permissions xxxx for 'xxxx' are too open.
- style/theme
- 平时遇到的一些坑,记录一下!!!!!!
- __clone()
- 从request获取各种路径总结
- Linux学习——文件共享
- HBase client buffer put为空 HTable
- 深入解析Python中的WSGI接口
- Unity里用Animator控制3dmax里制作的动画
- ViewConfiguration.getScaledTouchSlop () 用法
- Intellij IDEA中配置Maven
- 帅气的提交按钮
- Android的EventBus 详解(2.4及最新版3.0)[观察者模式]
- PHP写日志fwrite和file_put_contents的区别与性能
- 用genymotion安装应用出现INSTALL_FAILED_CPU_ABI_INCOMPATIBLE的错误
- Isomorphic Strings
- JavaScript中style.left与offsetLeft的使用及区别详解
- PE结构_01
- PHP基础
- __get __set