89. Gray Code
来源:互联网 发布:linux查找文件路径命令 编辑:程序博客网 时间:2024/06/16 06:14
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 - 001 - 111 - 310 - 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 List<Integer> grayCode(int n) { if(n == 0){ List<Integer> res = new ArrayList<>(); res.add(0); return res; } List<Integer> res = grayCode(n-1); int originSize = res.size(); int addN = 1 << (n-1); for(int i = originSize-1;i>=0;i--) res.add(addN+res.get(i)); return res; }}
阅读全文
0 0
- [LeetCode]89.Gray Code
- LeetCode 89. Gray Code
- [Leetcode] 89. Gray Code
- LeetCode --- 89. Gray Code
- [leetcode] 89.Gray Code
- 89. Gray Code
- 89. Gray Code
- 89. Gray Code LeetCode
- 89. Gray Code
- [LeetCode]89. Gray Code
- 89. Gray Code
- 89. Gray Code
- 89. Gray Code
- Leetcode 89. Gray Code
- [leetcode] 89. Gray Code
- 89. Gray Code
- LeetCode *** 89. Gray Code
- 89. Gray Code
- Babel 7步入门教程
- 房间安排
- Qt状态栏(statusbar)的使用
- etcd集群日常维护
- 网站被攻击了怎么办?常见的攻击又有哪些呢?
- 89. Gray Code
- 100个免费可商用字体,你总有一天用到它
- linux下svn服务器搭建及创建分支
- 读
- 从零基础到大数据专家要掌握的知识
- 数据类型
- Java多线程编程-(3)-线程本地ThreadLocal的介绍与使用
- 关于for each循环不能直接使用集合的remove方法的原因
- SQL基础语句