@@ -0,0 +1,1843 @@
+/**%0A *%09The gray code is a binary numeral system where two successive values differ in only one bit.%0A *%0A *%09Given 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.%0A *%0A *%09For example, given n = 2, return %5B0,1,3,2%5D. Its gray code sequence is:%0A *%0A *%0900 - 0%0A *%0901 - 1%0A *%0911 - 3%0A *%0910 - 2%0A *%0A *%09Note:%0A *  For a given n, a gray code sequence is not uniquely defined. %0A *%0A *%09For example, %5B0,2,3,1%5D is also a valid gray code sequence according to the above definition.%0A *%0A *%09For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.%0A *%0A *%09Author: matao%0A *%0A */%0A%0Aimport java.util.List;%0Aimport java.util.ArrayList;%0A%0Apublic class GrayCode %7B%0A%09public static List%3CInteger%3E grayCode(int n) %7B%0A%09%09List%3CInteger%3E result = new ArrayList%3CInteger%3E();%0A%09%09if (n %3C= 1) %7B%0A%09%09%09for (int i = 0; i %3C= n; i++) %7B%0A%09%09%09%09result.add(i);%0A%09%09%09%7D%0A%09%09%09return result;%0A%09%09%7D%0A%09%09result = grayCode(n - 1);%0A%09%09List%3CInteger%3E reverseResult = reverse(result);%0A%0A%09%09for(int i = 0; i %3C reverseResult.size(); i++) %7B%0A%09%09%09result.add((1 %3C%3C (n-1)) %7C reverseResult.get(i));%0A%09%09%7D%0A%09%09return result;%0A%09%7D%0A%0A%09public static List%3CInteger%3E reverse(List%3CInteger%3E list) %7B%0A%09%09List%3CInteger%3E rev = new ArrayList%3CInteger%3E();%0A%09%09for (int i = list.size() - 1; i %3E= 0; i--) %7B%0A%09%09%09rev.add(list.get(i));%0A%09%09%7D%0A%09%09return rev;%0A%09%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A    %09List%3CInteger%3E result = grayCode(1);%0A    %09for (Integer integer : result) %7B%0A    %09%09System.out.print(integer + %22 %22);%0A    %09%7D%0A    %09System.out.println();%0A%0A%09%09result = grayCode(2);%0A    %09for (Integer integer : result) %7B%0A    %09%09System.out.print(integer + %22 %22);%0A    %09%7D%0A    %09System.out.println();%0A%0A%09%09result = grayCode(3);%0A    %09for (Integer integer : result) %7B%0A    %09%09System.out.print(integer + %22 %22);%0A    %09%7D%0A    %09System.out.println();%0A    %7D%0A%7D

add Gray Code solution