[Leetcode-java] Gray Code

Source: Internet
Author: User
Tags binary to decimal

Topic:

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.

Original idea: Using the Carnot diagram to generate 2-binary gray code, and then converted to decimal.

Carnot diagram principle: The Gao Jiegre code can be obtained recursively by using only one change of the two adjacent lattices in the Carnot diagram and the Karnaugh map of the variable values in the order of lower order gray codes. This method is relatively cumbersome and less used. The steps to generate the gray code are as follows:

    1. The Carnot graph variable is divided into two groups with similar number of variables (preferably equal)
    2. Set up a Carnot diagram with a logical variable high at the left low in the right
    3. From the upper left corner of the Karnaugh map to the upper right corner to the bottom of the bottom to the left to traverse the Karnaugh map, sequentially through the lattice variable value is the typical gray code sequence
  Three-bit gray code (three-bit gray code built on two-bit basis)
Ab╲c 0 1
00 0→ 1↓
01 ↓2 ←3
11 6→ 7↓
10 4 ←5
Gray code sequence: 000 starting point 001 011 010 110 →111 101 100 EndFour-bit gray code
Ab╲cd 00 01 11 10
00 0→ 1→ 3→ 2↓
01 ↓4 ←5 ←7 ←6
11 12→ 13→ 15→ 14↓
10 8 ←9 ←11 ←10

Original code:

 Public classSolution { PublicList<integer> Graycode (intN) {List<Integer> req =NewArraylist<integer>(); String[] Gray=factory (n);  for(inti = 0; i < gray.length; i++) {String temp=Gray[i]; intToInt =Binarytoint (temp);        Req.add (ToInt); }                returnreq; }         PublicString[] Factory (intN) {//Build Gray Codestring[] req =Newstring[(int) Math.pow (2, N)]; if(n = = 0) {req[0] = "0"; returnreq; }        if(n = = 1) {req[0] = "0"; req[1] = "1"; returnreq; }        intup = N/2;//Small        intleft = n-up;//Bigstring[] Upstr=factory (UP); String[] Leftstr=factory (left); intLen = 0;  for(inti = 0; i < leftstr.length; i++){             for(intj = 0; J < Upstr.length; J + +) {Req[len]= Leftstr[i] +Upstr[j]; Len++; } I++;  for(intj = upstr.length-1; J >= 0; j--) {Req[len]= Leftstr[i] +Upstr[j]; Len++; }        }                returnreq; }         Public intBinarytoint (String str) {//2 binary to decimal        intCount = 0; intLen =str.length ();  for(inti = 0; i < Len; i++){            inttemp = Str.charat (i)-' 0 '; if(temp = = 0)                Continue; ElseCount+= (int) Math.pow (2,len-1-i); }        returncount; }}

Improved thinking: Using the new generation principle, you can see the N-bit gray code consists of two parts, part of the n-1-bit gray code, plus 1<< (n-1) and n-1 bit gray code in reverse order (the whole gray code reverse 0132 into 2310 this) and.

Original link: Http://www.cnblogs.com/springfor/p/3889222.html?utm_source=tuicool

Improved code:

 Public classSolution { PublicList<integer> Graycode (intN) {if(n==0) {List<Integer> req =NewArraylist<integer>(); Req.add (0); returnreq; } List<Integer> req = Graycode (n-1); intAddnumber = 1<<n-1; intPresize =req.size ();  for(inti = preSize-1; i>=0; i--) {Req.add (Addnumber+Req.get (i)); }                returnreq; }}

[Leetcode-java] Gray Code

Related Article

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.