Generate all gray codes for n-bit

Source: Internet
Author: User

original link: problem: Generate all gray codes for n-bits. Gray code is a collection of numbers, each of which is represented by a binary, assuming that an n-bit is used to represent each number, and that only one bit value is different between any two numbers. For example, the following is a 3-bit gray code: 000 001 011 010 110 111 101 100. If you want to generate the gray code of N-bit, then the number of gray code is 2^n. Assuming that the original value starts from 0, the rule of gray code is: the first step, change the rightmost bit value; The second step is to change the left bit of the first 1 bit to the right; the third step, the fourth step repeats the first and second steps Until all the gray codes have been generated (in other words, already gone (2^n)-1 steps). Use an example to illustrate: assuming a 3-bit gray code, the original value bit 000 The first step: Change the rightmost bit value: 001 Second step: Change the left bit of the first bit to 1:011 Step three: Change the rightmost bit value: 010 Fourth: Change the left bit of the first bit of the right up to 1: 110 Fifth Step: Change the rightmost bit value: 111 Sixth step: Change the left bit of the first 1 bit: 101 Seventh Step: Change the rightmost bit value: 100 If you follow this rule to generate the gray code, there is no problem, but it is too complicated. If you look closely at the structure of the Gray code, we will have the following findings: 1. Except for the highest bit (the first bit on the left), the bit of gray code is completely up and down (see list below). For example, the first gray code and the last Gray code symmetry (except the first one), the second Gray code and the penultimate symmetry, and so on. 2. the smallest repeating unit is 0, 1。 000
100 so, when implemented, we can fully use recursion, add 0 or 1 in front of each layer, and then we can list all the gray codes. For example: The first step: Generate 0, 12 strings. The second step: on the basis of the first step, each string is added 0 and 1, but can only add one at a time, so you have to do two times. This becomes the 00,01,11,10 (attention symmetry). Step three: On the basis of the second step, each string is added 0 and 1, again, can only add one at a time, so it becomes 000,001,011,010,110,111,101,100. OK, so we can generate 3-bit gray code. To generate a 4-bit gray code, we just need to add another layer of 0,1 on the 3-bit gray code: 0000,0001,0011,0010,0110,0111,0101,0100,1100,1101,1110,1010,0111,1001,1000. In other words, The N-bit gray code is generated based on the n-1 bit-coded gray code. Solution 1:
classGraycode { Public: Vector<string> Getgray (intN) {//Write code herevector<string> V (POW (2, N)); if(n = =1) {v[0] ="0"; v[1] ="1"; returnv; } Vector<string> last = Getgray (n-1);  for(inti =0; I < last.size (); ++i) {V[i]="0"+Last[i]; V[v.size ()-1-I] ="1"+Last[i]; }                returnv; }};

Solution 2:

Generate all gray codes for n-bit

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