[LeetCode] Gray Code

Source: Internet
Author: User

[LeetCode] Gray Code

The gray code is a binary numeral system where two successive values 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]Is also a valid gray code sequence according to the above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.


Refer to Gray Code


public class Solution {    public List
 
   grayCode(int n) {    List
  
    list = new ArrayList<>();    List
   
     str = help(n);    for(int i=0;i
    
      help(int n){    List
     
       list = new ArrayList<>();    if(n==0) {    list.add("0");    return list;    }    if(n==1){    list.add("0");    list.add("1");    return list;    }    List
      
        lin = help(n-1); List
       
         res = new ArrayList<>(); for(int i=0;i
        
         =0;i--){ String str = "1"+lin.get(i); res.add(str); } return res; }}
        
       
      
     
    
   
  
 




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.