Title:
The gray code is a binary numeral system where the successive values are 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, was [0,2,3,1]
also a valid gray code sequence according to the above definition.
For now, the judge are able to judge based on one instance of gray code sequence. Sorry about that.
Answer 1: Mapping method
Idea: We observed the gray code and found that the Gray code has a mapping relationship. The gray code of N-bit can be obtained quickly from the gray code of the n-1 bit with the new bit after the mirror shot (half order, half reverse, plus 1<<n). We start with a bit of gray code, until we get n bits of gray code. The first bit is 0 + 1<<0, 0 + 1<<1.
Attention:
1.2^n can be expressed as 1<<n.
<span style= "FONT-SIZE:14PX;" > Curnum + = (1 << i);</span>
2. When n takes 0 o'clock, returns {0} instead of an empty array.
<span style= "FONT-SIZE:14PX;" >vector<int> ret{0};</span>
complexity: O (n^2)
AC Code:
<span style= "FONT-SIZE:14PX;" >class Solution {public: vector<int> graycode (int n) { vector<int> ret{0}; for (int i = 0; i < n; i++) { int curcnt = Ret.size (); Add the current number in reverse order to the RET while (curcnt) { curcnt--; int curnum = ret[curcnt]; Curnum + = (1 << i); Ret.push_back (Curnum); } } return ret; }}; </span>
Answer 2: Binary transcoding of gray code
idea: by observing the binary and gray codes, we get the formula: Gray = (binary) XOR (binary >> 1). There is a detailed explanation in this article.
below we write both the binary number and the gray code are below, you can see the left of the number of different or the right side of the result is equal to the number of the right.
Binary number gray code
000 000
001 001
010 011
011 010
100 110
101 111
110 101
111
from the binary number point of view, the number on the "mirror" position is the result of a not operation on the original number. For example, each of the 3rd number 010 and the 3rd Digit 101 is the exact opposite. Assuming that the two numbers are X and y respectively, then the result of X XOR (x shr 1) and y xor (y shr 1) is only a little different: the first of the latter is 1, the first of the former is 0. And this is exactly how the gray code is generated. This shows that the nth number of gray code is really n xor (n shr 1).
Attention:
1.1 << n bit, bit is n+1 bit, just represents n bit binary number +1, such as three bit binary maximum is 7, 1<<3 = 8. So we can get the range of the number we require as a cyclic condition.
<span style= "FONT-SIZE:14PX;" >for (int i = 0; i < num; i++) </span>
AC Code:
<span style= "FONT-SIZE:14PX;" >class Solution {public: vector<int> graycode (int n) { int num = 1 << n; vector<int> ret; Ret.reserve (num); for (int i = 0; i < num; i++) { Ret.push_back (i ^ (i >> 1)); } return ret; }}; </span>
[C + +] Leetcode:86 Gray Code (grey code)