题解 | #生成格雷码#
生成格雷码
https://www.nowcoder.com/practice/50959b5325c94079a391538c04267e15
代码实现:
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; } }