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