89. 格雷编码
Java代码:2DFS
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> grayCode(int n) {
dfs(n, new StringBuffer(), new int[]{0, 1});
return res;
}
public void dfs(int n, StringBuffer sb, int[] nums){
if(sb.length() == n){
res.add(Integer.valueOf(sb.toString(), 2));
return;
}
sb.append(nums[0]);
dfs(n, sb, new int[]{0,1});
sb.deleteCharAt(sb.length() - 1);
sb.append(nums[1]);
dfs(n, sb, new int[]{1, 0});
sb.deleteCharAt(sb.length() - 1);
}
}