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