leetcode---Gray Code
来源:互联网 发布:网络小精灵 编辑:程序博客网 时间:2024/05/16 10:17
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.
class Solution {public: vector<int> grayCode(int n) { vector<int> result; result.push_back(0); for(int i=0; i<n; i++) { int h = 1 << i; for(int j = result.size()-1; j >= 0; j--) result.push_back(h + result[j]); } return result; }};
0 0
- LeetCode: Gray Code
- [LeetCode] 格雷码(Gray Code)
- LeetCode Gray Code
- [Leetcode] Gray Code
- [LeetCode] Gray Code
- leetcode 112: Gray Code
- LeetCode Gray Code
- [leetCode] Gray Code
- [LeetCode]Gray Code
- Leetcode: Gray Code
- [leetcode]Gray Code
- LeetCode-Gray Code
- [leetcode] Gray Code
- LeetCode - Gray Code
- leetcode之Gray code
- LeetCode:Gray Code
- 【转载】【leetcode】Gray Code
- LeetCode - Gray Code
- 剑指offer系列-T27把二元查找树转变成排序的双向链表
- UUID数据结构和使用方法
- Binary Tree Vertical Order Traversal
- Wildcard Matching
- 证据理论
- leetcode---Gray Code
- iOS 【iOS中常用的几种库 .a & .framework】
- Java轻量级组件和重量级组件
- Matlab Legend(转载)
- GitHub 20分钟教你学会使用熟练
- ulua经验
- iOS 【离屏渲染】
- JavaScript实现输入框动态显示光标的XY值
- 算法基础篇:①最短路 路径还原(Dijkstra输出完整方案)②最小生成树prim算法和kruskal算法解析