Old discuss is read-only now. Please go to New LeetCode Discuss for your questions and answers!

User account in old discuss will not integrate to the new one, but new discuss is integrated with new online judge, which means, if you already have an account in new online judge, you can access new discuss immediately!

If you want to ask for question relevant to the posts in old discuss, please copy content as part of your question, only old discuss link is NOT ALLOWED!

Please read the FAQ in new LeetCode Discuss to help yourself making the best use of Discuss!

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.

asked 20 May '12, 16:20

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k1384171
accept rate: 2%

closed 25 Oct '13, 06:41

The question has been closed for the following reason "bulk close" by 1337c0d3r 25 Oct '13, 06:41


10
class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> ret;
        int count = 0x01 << n;
        for(int i = 0 ; i < count; ++i) {
            ret.push_back(i ^ (i>>1));
        }
        return ret;
    }
};
link

answered 31 Dec '12, 06:18

unieagle's gravatar image

unieagle
19633
accept rate: 0%

vector<int> grayCode(int n) {
    vector<int> r;
    r.push_back(0);
    for(int i = 0; i < n; ++i)
        for(int j = r.size() - 1; j >= 0; --j)
            r.push_back(r[j] | 1 << i);
    return r;
}
link

answered 31 Dec '12, 18:59

luckynoob's gravatar image

luckynoob
54746
accept rate: 0%

explain to these code: 00 000 01 001 --- -> 011 11 010 10 ----- 110 111 101 100

(07 Jan '13, 08:16) sillyjims sillyjims's gravatar image

A recursive solution in java

public ArrayList<Integer> grayCode(int n) {  
    ArrayList<Integer> resultList = new ArrayList<Integer>();
    if (n <= 1) {
        resultList.add(0);
        if (n == 1) resultList.add(1);
        return resultList;
    }

    ArrayList<Integer> prevList = grayCode(n - 1);
    int highest_bit = 1 << (n - 1);
    for (int i = prevList.size() - 1; i >= 0; i--) {
        resultList.add(prevList.get(i) + highest_bit);
    }

    prevList.addAll(resultList);
    return prevList;
}
link

answered 21 Jan '13, 09:19

lvlv's gravatar image

lvlv
864
accept rate: 0%

edited 21 Jan '13, 09:19

This is nice for interview solution. The i ^(i >> 1) is too subtle to show.

(21 Mar '13, 07:26) etlds etlds's gravatar image
class Solution 
{
public:
  vector<int> grayCode(int n) 
 {
  // Start typing your C/C++ solution below
  // DO NOT write int main() function
  vector<int> r;
  int num = 1 << n;
  for(int i=0;i<num;i++)
  {
    r.push_back(i ^ (i>>1));
  }
  return r;
 } 
};
link
This answer is marked "community wiki".

answered 22 Jan '13, 21:39

spooky's gravatar image

spooky
124
accept rate: 0%

class Solution {
public:
vector<int> grayCode(int n) {
  vector<int> vec;
    int loop = (1<<n);
    for (int i = 0; i < loop; i++)
        vec.push_back(i^(i>>1));
    return vec;
}
};
link

answered 26 Jan '13, 19:59

BlackMamba's gravatar image

BlackMamba
11124
accept rate: 0%

edited 26 Jan '13, 19:59

vector<int> grayCode(int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
   vector<int> ret;
   ret.push_back(0);
   int mask = pow(2, n)-1;
   int visit = (int(pow(2, n)) & mask), i=0, cur = 1, x = 0;
   while(i < mask)
   {
        while(( (visit >> (cur-1)) & 1 ) != 0)
        {
            cur ++;
        }

       x = (x ^ (1 << (cur-1))) & mask;    // make cur pos in x as opposite, 0->1, 1->0
       visit = visit & ~((1 << cur) - 1);  // make the bit to the left of cur as 0, which is not visited
       visit = visit | (1 << (cur-1));     // make cur as 1, which is visited
       ret.push_back(x);
       cur = 1;                            // cur back to 1, ready to change from the lowest bit

       i++;      
   }
   return ret;
}
link

answered 08 Jun '13, 01:17

ddd332's gravatar image

ddd332
11
accept rate: 0%

An alternative solution using bit operations

public class Solution {       
    public ArrayList<Integer> grayCode(int n) {
        ArrayList<Integer> ans=new ArrayList<Integer>();
        for (int i=0;i<1<<n;i++)
            ans.add(0);
            for (int i=0;i<n;i++){
                for (int j=0;j<ans.size();j++){
                    int exp=0^((j>>i)&1) ^(j>>(i+1) &1);
                    ans.set(j, ans.get(j)+(1<<i)*exp);
                }
            }        
        return ans;
    }
}
link

answered 10 Sep '13, 08:48

gnarl's gravatar image

gnarl
11
accept rate: 0%


//recursive approach in C++
std::vector<int> v;
std::vector<int> grayCode(int n) {
    v.clear();
    if (n >= 0) v.push_back(0);
    if (n >= 1) v.push_back(1);
    int k = 1;
    while (k < n) {
        unsigned int highestBit = 1 << k;
        int sz = v.size();
        for (int i = sz-1; i >= 0; --i) {
            v.push_back(v[i]|highestBit);
        }
        ++k;
    }       
    return v;
}
link

answered 18 Oct '13, 21:33

dzab's gravatar image

dzab
113
accept rate: 0%

edited 18 Oct '13, 21:54

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×133

Asked: 20 May '12, 16:20

Seen: 2,889 times

Last updated: 25 Oct '13, 06:41