代码实现:
import java.util.*; public class GrayCode { public String[] getGray(int n) { // write code here String[] ans = new String[(int) Math.pow(2, n)]; ans[0] = "0"; ans[1] = "1"; int len = 2, len2 = len + len; for(int i = 1; i < n; i++) { for(int j = len; j < len2; j++) { ans[j] = "1" + ans[len - 1 - j + len]; } for(int j = 0; j < len; j++) { ans[j] = "0" + ans[j]; } len = len2; len2 = len + len; } return ans; } }