Leetcode---89. Gray Code

Source: Internet
Author: User

Title Link: Gray Code

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:

Note:

For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] was 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.

The requirement of this problem is to generate all n-bit gray codes sequentially.

In the code of a group of numbers, if any two adjacent code is different from only one binary number, it is called the Gray Code, in addition, because the maximum number and the minimum number is only one digit difference, namely "end-to-end", so also called cyclic code or reflection code. In a digital system, code is often required to change in a certain order. For example, by increasing the number of natural numbers, if the number of 8421 yards, 0111 to 1000 when the four-bit changes, and in the actual circuit, 4-bit changes can not happen absolutely simultaneously, the count may appear in the short-term other code (1100, 1111, etc.). In certain cases, the circuit status error or input error may be caused. This error can be avoided by using gray code. Gray code is available in a variety of coding formats.

Gray code has used grey code, Grimes Code, code, Golay code, cyclic code, reflective binary code, the smallest error code and other names, some of them are wrong, some easily confused with other names, it is recommended not to use these used names.

Direct arrangement

The gray code with binary value of 0 is the 0th item, the first one changes the rightmost bit, the second changes the left bit of the first bit to 1, the third to fourth method and the first to second item, so repeatedly, we can arrange the gray code of n bits.

Mirror arrangement

The gray code of N-bit can be obtained quickly from the gray code of the n-1 bit by adding the new bit after the mirror shot. This method is based on the fact that Gray code is a reflection code, using the following rules of recursion to construct:

    1. 1-bit gray code with two code words
    2. (n+1) Bit gray code in the first 2^n code word equals n-bit gray code word, in order to write, prefix 0
    3. (n+1) Bit gray code in the latter 2^n code word equals n-bit gray code word, in reverse order, plus prefix 1

Binary number transcoding Gray code

(assuming that the value of binary 0 is 0 of the Gray code)

G: Gray code B: binary Code

G (n) = (b (n)/2) XOR B (n)

Gray transcoding binary number

Binary code Nth bit = binary code (n+1) bit + gray code nth bit. Since both the binary code and the gray code have the same number of digits, the binary code can be calculated by taking 0 from the top left bit. (Note: Results are treated as 0 when you encounter a ")

For example: Gray code 0111, is 4 digits, so its binary code will be converted to 4-digit number, it is advisable to turn into the binary code fifth bit 0, that is, 0 B3 B2 B1 b0.

0+0=0,所以b3=00+1=1,所以b2=11+1取0,所以b1=00+1取1,所以b0=1

So the binary code is converted to 0101.

Reference from Wikipedia.

At last

This problem requires the generation of n-bit gray code, a total of 2^n. All gray codes can be generated incrementally based on the formula g (n) = B (n) XOR (b (n)/2) above.

Complexity of Time: O (2^n)

Space complexity: O (2^n)

1 class Solution 2 {3  Public:4     Vector<int> Graycode(int N) 5     {6         Vector<int> v;7          for(int I = 0; I < 1 << N; ++ I)8             v.push_back(I ^ I >> 1);9         return v;Ten     } One };

Reprint please indicate source: Leetcode---89. Gray Code

Leetcode---89. Gray Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.