leetcode 89:Gray Code
2015-07-21 10:09
295 查看
转:https://github.com/soulmachine/leetcode
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
is:
Note:
For a given n, a gray code sequence is not uniquely defined.
For example,
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
格雷码(Gray Code) 的定义请参考https://en.wikipedia.org/wiki/Gray_code
这题要求生成n 比特的所有格雷码。
方法1,最简单的方法,利用数学公式,对从0 2^n - 1) 的所有整数,转化为格雷码。
方法2,n 比特的格雷码,可以递归地从n -1 比特的格雷码生成。
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.
格雷码(Gray Code) 的定义请参考https://en.wikipedia.org/wiki/Gray_code
这题要求生成n 比特的所有格雷码。
方法1,最简单的方法,利用数学公式,对从0 2^n - 1) 的所有整数,转化为格雷码。
方法2,n 比特的格雷码,可以递归地从n -1 比特的格雷码生成。
相关文章推荐
- AM335X Nand Flash 启动调试
- Mac OS X上使用Wireshark抓包
- final的作用
- 一个对象实例化过程
- poj 3087 Shuffle'm Up
- 【设计模式】装饰模式与继承的不同
- 系统集成项目管理工程师(软考中级)已过,写写经历
- OpenGL关于二维变换的例子
- 第三章 JS基本概念
- Oracle的update语句优化研究
- [转载] YouCompleteMe
- Android中捕捉menu按键点击事件的方法
- Java面试-Hibernate总结
- 修改分隔符之后不能正常使用hasNextInt()函数
- 移动方法
- PL/SQL相关的数据字典
- 动态内表实例
- 《JavaScript高级程序设计(第三版)》读书笔记
- sgu109 Magic of David Copperfield II
- oracle中merge into的用法