[leetcode#89] Gray Code

Source: Internet
Author: User

The problem:

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.

My Analysis:

the solution behind is problem are very tricky, but elegant. The wiki link forThe solution:http://zh.wikipedia.org/wiki/Gray Code #mediaviewer/file:binary-reflected_gray_code_construction.svgThe key idea:n' s Gray code collection could is infered from N-1 's Gray code by following:1. First, we add ' 0 ' at the front of all gray Code of N-1. Since the' 0 ' would not change the value of the gray code, we could just keep the n-1 ' s gray integer.2. Second, we reverse the n-1 ' s gray code collection, and add ' 1 ' at the front of all gray code of n-1.the method is very tricky, since the reverse can guarantee the both middle elements have the same gray code SERIES.00001 00110 01011 <---The same 011 <---PR                      efix:011 <---The same 111 <---prefix:110 11001 10100 100What a tricky method!!!The code for  Thisis : for(inti = 2; I <= N; i++) {     for(intj = ret.size ()-1; J >= 0; j--) {Ret.add (Ret.get (j)+ (1 << i-1)); }}facts:1. We won ' t delete the n-1 's Gray Iteger.2The . We scan the ArrayList from the end to the start. A Little skill:how to get the Interger value of n digits, and only the nth digit' s value is 1, and other's is 0.9 digits:100000000 1 << 8n digits:1 << n-1 (This is a very important skill!!! Not 1 << N).

My Solution:

 Public classSolution { PublicList<integer> Graycode (intN) {ArrayList<Integer> ret =NewArraylist<integer> (); if(N < 0 | | n > 32)            returnret; if(n = = 0) {Ret.add (0); returnret; } ret.add (0); Ret.add (1);  for(inti = 2; I <= N; i++) {             for(intj = ret.size ()-1; J >= 0; j--) {Ret.add (Ret.get (j)+ (1 << i-1)); }        }        returnret; }}

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