LeetCode GrayCode


class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
res.push_back(0);

long pow2 = 1;
for (int i=1; i <= n; i++) {
int old_len = res.size();
for (int i=0; i<old_len; i++) {
res.push_back(pow2 + res[old_len - i - 1]);
}
pow2<<=1;
}
return res;
}
};

再水一发, 不过n==0时,觉得应该返回一个空的vector

时间: 2024-10-16 20:44:00

LeetCode GrayCode的相关文章

LeetCode:GrayCode

题目描述: 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

LeetCode:Gray Code

1.题目名称 Gray Code(格雷码) 2.题目地址 https://leetcode.com/problems/gray-code/ 3.题目内容 英文: 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

LeetCode --- 89. Gray Code

题目链接:Gray Code 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 b

leetcode第一刷_Gray Code

说到格雷码,应该没人不知道,具体它有什么用,我还真不是很清楚,我室友应该是专家.生成的规律不是很明显,之前看到帖子讲的,这会儿找找不到了.. 思想是这样的,如果有n位,在第2^(n-1)个编码下面画一条水平线的话,你会发现除了第一位之外,其他位都是关于这条线对称的,如下,以三位格雷码举例: 000 001 011 010 --------------------- 110 111 101 100 很神奇吧,我以前是不知道这个规律的.从一开始的一位格雷码,0,1,开始,每次对称的在上一个前面添加上

[LeetCode][JavaScript]Gray Code

Gray Code 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

LeetCode——Gray Code

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. Fo

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

Gray Code leetcode java

题目: 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

【一天一道LeetCode】#89. Gray Code

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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