0
点赞
收藏
分享

微信扫一扫

Leetcode89. 格雷编码


题目传送:​​https://leetcode.cn/problems/gray-code/​​

运行效率:

Leetcode89. 格雷编码_找规律


这道题说白了就是找规律。记公式就行

Leetcode89. 格雷编码_数据结构_02

代码如下:

class Solution {
public static List<Integer> grayCode(int n) {
List<Integer> result = new ArrayList<>();
//处理边界情况
if (n == 1) {
result.add(0);
result.add(1);
return result;
}
//
List<Integer> list = grayCode(n - 1);
result.addAll(list);
for(int i=list.size()-1;i>=0;i--){
Integer num = list.get(i);
num = num + (int) Math.pow(2, n - 1);
result.add(num);
}

return result;
}
}


举报

相关推荐

0 条评论