解法一:格雷码
满足这种规律很容易想到格雷码,关于格雷码的实现:格雷码。
看了评论区大佬的才知道可以对数组元素进行移动,这样就解决了以start为第一个数的要求。
class Solution {public List<Integer> circularPermutation(int n, int start) {int len = 1 << n;List<Integer> ans = new ArrayList<>(len);for (int i = 0; i < len; ++i) {ans.add(i ^ (i >> 1));}for (int i = 0; i < len; ++i) {if (ans.get(i) == start) {Collections.rotate(ans, -i);break;}}return ans;}}
