package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

/**
 * 89. 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 - 0
 01 - 1
 11 - 3
 10 - 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.
 */

public class _89 {

    public List<Integer> grayCode(int n) {
        List<Integer> result = new ArrayList();
        for (int i = 0; i < (1<<n); i++){
            result.add(i ^ (i>>1));
        }
        return result;
    }

}

@@ -29,16 +29,66 @@
 tions;%0A%0A
+import com.fishercoder.common.utils.CommonUtils;%0A%0A
 import j
@@ -107,16 +107,16 @@
 ayList;%0A
-
 import j
@@ -251,17 +251,18 @@
 ne bit.%0A
-%0A
+ *
  Given a
@@ -335,16 +335,19 @@
 he code,
+%0A *
  print t
@@ -371,16 +371,19 @@
 ay code.
+%0A *
  A gray 
@@ -525,15 +525,17 @@
 - 2%0A
+%0A
  Note:%0A
+%0A
  For
@@ -1022,10 +1022,251 @@
 %0A    %7D%0A%0A
+    public static void main(String... args) %7B%0A        int n = 3;%0A        System.out.println(%221 %3C%3C n = %22 + (1 %3C%3C n));%0A        _89 test = new _89();%0A        List%3CInteger%3E result = test.grayCode(n);%0A        CommonUtils.printList(result);%0A    %7D%0A
 %7D%0A

refactor 89