Leetcode:gray Code

Source: Internet
Author: User

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

Gray Code:
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. Gray Code was originally intended for communication and is now commonly used in analog-to-digital conversions and position-to-digital conversions.

Gray Code Conversion Method:
Recursive generation Code table
This method is based on the fact that Gray code is a reflection code, using the following rules of recursion to construct:





C + + Reference code (spents 7ms):

classsolution{ Public: vector<int>Graycode (intN) { vector<int>Codes Codes.push_back (0); for(inti =0; I < n; ++i) {intone =1<< i;//Highest digit 1            intSize =int(Codes.size ());//This cycle should be reversed!             for(intj = Size-1; J >=0;            --J) {codes.push_back (one + codes[j]); }        }returnCodes }};

xor or conversion
Binary code, gray Code (code):




Gray code--binary code (decoding):
From the second position on the left, the decoded value of each bit and the left one is the same as the decoded value of the bit (the leftmost one remains unchanged).

C + + code (spents 9ms):

class  solution{public : vector  << Span class= "Hljs-keyword" >int ;  graycode (int  N) {vector  <int  >         codes; int  size = 1  << N;        //equivalent to POW (2, N)  for         (int  i = 0 ; i < size; ++i)        {Codes.push_back ((I >> 1 ) ^ i);    } return  codes; }};

I found that moving the right of line tenth one codes.push_back ((i >> 1) ^ i) (9ms) to the Division Operation Codes.push_back ((I/2) ^ i) (6ms) also became faster!

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