[Leetcode]89 Gray Code

Source: Internet
Author: User

https://oj.leetcode.com/problems/gray-code/

Public class solution {    public list<integer> graycode ( Int n)      {        //  Law:         // n = 0:         // 0        //         // n = 1:        // 0         // 1        //         // n = 2:        //  00        // 01        //  11        // 10         //        // n = 3        // 000         // 001        //  011        // 010         // 110        // 111         // 101        // 100         //        //  Set  n-1  result set   for  s        //  positive order   each  i, add  0 to the head and put in a new result set         //  reverse   Each  i, add a  1 to the head and put in a new result set          if  (n < 0)              return collections.emptylist ();                     if  (n == 0)              return collections.singletonlist (0);                 list<string> str = code (n);         List<Integer> toReturn = new  Arraylist<> ();        for  (STRING&NBSP;S&NBSP;:&NBSP;STR)         {             toreturn.add (Integer.parseint (s, 2));        }         return toReturn;    }         prIvate list<string> code (int n)     {         if  (n == 1)         {             list<string> toreturn = new  ArrayList<> (            toreturn.add); ("0");             toreturn.add ("1");             return toReturn;         }                 list<string> last = code (n - 1);         List<String> toReturn = new ArrayList<> ();         //&nBsp;for each e in last, append 0, to head. and into new  result        for  (string s : last)          {             toreturn.add ("0"  + s);        }         // for each e in last  (reverse), and  append  (1)  in it.        for  (int i =  last.size ()  - 1 ; i >= 0 ; i --)          {            toreturn.add ( "1"  + last.get (i));        }         return toreturn;    }} 


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