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.

公式:

G(i) = i^ (i/2).

 class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
for(int i =0;i<1<<n;i++)
res.add(i^i>>1);
return res;
}
}

最新文章

  1. backbone学习总结(二)
  2. jeecg小吐槽续——自己折腾修改在线开发功能中“默认值”的使用
  3. transition
  4. Unity 小问题
  5. 如何用SQL语句实现Mysql数据库的备份与还原
  6. SpringMVC国际化
  7. wsdl中含ref=&quot;s:schema&quot;时处理的
  8. jquery点击改变class并toggle
  9. HDU1001
  10. C# 合并DLL, 合并DLL进入EXE 【转】
  11. linux配置记录
  12. Surrounded Regions——LeetCode
  13. SqlServer跨域查询
  14. Centos7.3下mysql5.7.18安装并修改初始密码的方法
  15. DirectX:函数可以连接任意两个filter
  16. 火狐浏览器安装firebug和firepath插件方法(离线)
  17. Thinkphp5.0支付宝支付扩展库类库大全
  18. 浅谈Spring
  19. ZOJ - 1610 经典线段树染色问题
  20. java新特性

热门文章

  1. swif开发之--协议的使用
  2. NodeJS-002-Expres启动
  3. Python3 抓取豆瓣电影Top250
  4. linux系统UDP的socket通信编程
  5. 我们复习.Net的这些日子里
  6. 基于Cocos2d-x学习OpenGL ES 2.0系列——初识MVP(3)
  7. php学习十四:抽象,接口和多态
  8. iOS开发 - 检测网络状态(WIFI、2G/3G/4G)
  9. Android英文文档翻译系列(2)——HandlerThread
  10. UEditor整合代码高亮插件SyntaxHighlighter