Gray Code Leetcode Java

Source: Internet
Author: User

Describe
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-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] 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.
Analysis
For the definition of gray code, please refer to Wikipedia http://en.wikipedia.org/wiki/Gray_code.
Natural binary code converted to Gray code: G0 = B0, gi = bi⊕bi?1
Keep the highest bit of natural binary code as the highest bit of gray code, Gray code sub-high is the binary code of the high and sub-high
Or, the rest of you are similar to the second-highest method of seeking. For example, the process of converting natural binary code 1001 to Gray code is: preserving the most
High, then the 1th bit of 1 and 2nd bits of 0 xor, get 1, as the Gray code 2nd bit; 2nd bit 0 and 3rd bit
The 0 xor, get 0, as the 3rd bit of gray code; the 3rd digit 0 and 4th bit 1 xor, get 1, as gray code
4th place, finally, Gray code is 1101.
Gray code converted to natural binary code: B0 = g0, bi = gi⊕bi?1
Keep the highest bit of gray code as the highest bit of natural binary code, the second high is the natural binary high and Gray code Sub High
Or, the rest of you are similar to the second-highest method of seeking. For example, the process of converting Gray Code 1000 to Natural binary code is: retain the highest
Bit 1, as the highest bit of natural binary code, then the natural binary code of the 1th bit 1 and gray code 2nd bit 0 XOR, get
1, as the natural binary code of the 2nd place; the natural binary code of the 2nd bit 1 and Gray code 3rd bit 0 xor, get 1, for
is the 3rd digit of the natural binary code; the 3rd digit 1 of the natural binary code and the 4th bit of the Gray code 0 XOR, get 1, as a natural
The 4th digit of the binary code, and finally, the natural binary code is 1111.
Gray code has a mathematical formula, the gray Code of the integer n is n⊕ (N/2).
This requires generating all the gray codes for n-bits.
The simplest way to use a mathematical formula, on a 0 to 2
N? 1 of all integers, converted to gray code.
N-bit gray code, can be recursively from n? 1-bit gray code generation.

This question I have been tossing for a long time, reluctantly passed. But occasionally see a more concise method (@ Love to cook small jade-like), in the construction of the array with recursion. For the convenience of their own study, so also posted up.

Code:

1 Importjava.util.ArrayList;2 3  Public classGraycode {4 5      Public Static voidMain (string[] args) {6                  intN=3;7 System.out.println (Graycode (n));8     }9       Public StaticArraylist<integer> Graycode (intN) {Ten                  if(n==0) {   Onearraylist<integer> result =NewArraylist<integer>();  AResult.add (0);  -                       returnresult;  -                   }   the                      -arraylist<integer> result = Graycode (n-1); - //arraylist<integer> result = new arraylist<integer> (n); -                   intAddnumber = 1 << (n-1); +                  intAsize=result.size (); -                   +                   for(inti=asize-1;i>=0;i--) {   AResult.add (Addnumber +Result.get (i));  at                  }   -                  returnresult;  -              } -}

Then I saw the simpler way. Gray code, binary transcoding method is the binary pre-complement 0, and then two-bit XOR. GI = bi ^ bi+1 such as binary 0101, before adding 0 after 00101,

Then the right-to-left 22-bit XOR 0111 (the difference is 1, the same as 0), get Gray code 0111, in fact, is the binary x Do (x>>1) ^x operation.

1  Public classSolution {2      PublicList Graycode (intN) {3List List =NewArrayList ();4         if(n<0) {5             returnlist;6         }7          for(inti=0; i< (1<<n); i++) {8List.add ((i>>1) ^i);9         }Ten         returnlist; One     } A}

Gray Code Leetcode Java

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.