89. Gray Code (Bit)
2015-12-05 11:46
267 查看
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
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.
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
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.
class Solution { public: vector<int> grayCode(int n) { vector<int> result; int size = 1 << n; for (int i =0; i< size; i++) { result.push_back(getGrayCode(i)); } return result; } int getGrayCode(int binary) { return ((binary >> 1)^binary); } };
相关文章推荐
- ExcelToLua工具
- Pycharm 安装 , Git 插件安装,django环境配置
- numpy.random.shuffle(x)的用法
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- python基本 tuple
- Codevs_P1425 最长公共子串(KMP)
- android(20)(从网络抓取html显示在本地,把流转换成String,请求网络,简单的消息处理机制,编码问题)
- 加入隐式反馈的矩阵分解
- web前台工作笔记1
- Linux Is Not Matrix——repo浅解
- Unity3D研究院之Android NDK编译C/C++结合Unity实现本地数据共享(二十八)
- Java基础2——集合框架
- Spring MVC的基本概念
- Android 自定义属性的使用
- Linux串口驱动(8250)的编写与调试
- 12月风怒(2)
- Java中使用PrepateStatement并且like模糊查询
- James邮箱服务器账户资料迁移问题
- 黑马程序员——数据类型、printf函数与scanf函数
- Installation error:INSTALL_FAILED_VERSION_DOWNGRADE