classSolution { public: vector<int> circularPermutation(int n, int start){ vector<int> ret; ret.reserve(1 << n); ret.push_back(start); for (int i = 1; i <= n; i++) { int m = ret.size(); for (int j = m - 1; j >= 0; j--) { ret.push_back(((ret[j] ^ start) | (1 << (i - 1))) ^ start); } } return ret; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public List<Integer> circularPermutation(int n, int start) { List<Integer> ret = newArrayList<Integer>(); ret.add(start); for (inti=1; i <= n; i++) { intm= ret.size(); for (intj= m - 1; j >= 0; j--) { ret.add(((ret.get(j) ^ start) | (1 << (i - 1))) ^ start); } } return ret; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSolution { public IList<int> CircularPermutation(int n, int start) { IList<int> ret = new List<int>(); ret.Add(start); for (int i = 1; i <= n; i++) { int m = ret.Count; for (int j = m - 1; j >= 0; j--) { ret.Add(((ret[j] ^ start) | (1 << (i - 1))) ^ start); } } return ret; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
int* circularPermutation(int n, int start, int* returnSize){ int *ret = (int *)malloc((1 << n) * sizeof(int)); ret[0] = start; int ret_size = 1; for (int i = 1; i <= n; i++) { for (int j = ret_size - 1; j >= 0; j--) { ret[2 * ret_size - 1 - j] = ((ret[j] ^ start) | (1 << (i - 1))) ^ start; } ret_size <<= 1; } *returnSize = ret_size; return ret; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10
funccircularPermutation(n int, start int) []int { ans := make([]int, 1, 1<<n) ans[0] = start for i := 1; i <= n; i++ { for j := len(ans) - 1; j >= 0; j-- { ans = append(ans, ((ans[j]^start)|(1<<(i-1)))^start) } } return ans }
[sol1-Python3]
1 2 3 4 5 6 7
classSolution: defcircularPermutation(self, n: int, start: int) -> List[int]: ans = [start] for i inrange(1, n + 1): for j inrange(len(ans) - 1, -1, -1): ans.append(((ans[j] ^ start) | (1 << (i - 1))) ^ start) return ans
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10
var circularPermutation = function(n, start) { const ret = [start]; for (let i = 1; i <= n; i++) { const m = ret.length; for (let j = m - 1; j >= 0; j--) { ret.push(((ret[j] ^ start) | (1 << (i - 1))) ^ start); } } return ret; };
复杂度分析
时间复杂度:O(2^n)。每一个格雷码生成的时间为 O(1),总时间为 O(2^n)。
空间复杂度:O(1)。这里返回值不计入空间复杂度。
方法二:公式法
代码
[sol2-C++]
1 2 3 4 5 6 7 8 9 10
classSolution { public: vector<int> circularPermutation(int n, int start){ vector<int> ret(1 << n); for (int i = 0; i < ret.size(); i++) { ret[i] = (i >> 1) ^ i ^ start; } return ret; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9
classSolution { public List<Integer> circularPermutation(int n, int start) { List<Integer> ret = newArrayList<Integer>(); for (inti=0; i < 1 << n; i++) { ret.add((i >> 1) ^ i ^ start); } return ret; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9
publicclassSolution { public IList<int> CircularPermutation(int n, int start) { IList<int> ret = new List<int>(); for (int i = 0; i < 1 << n; i++) { ret.Add((i >> 1) ^ i ^ start); } return ret; } }
[sol2-C]
1 2 3 4 5 6 7 8 9
int* circularPermutation(int n, int start, int* returnSize){ int ret_size = 1 << n; int *ret = (int *)malloc(ret_size * sizeof(int)); for (int i = 0; i < ret_size; i++) { ret[i] = (i >> 1) ^ i ^ start; } *returnSize = ret_size; return ret; }
[sol2-Golang]
1 2 3 4 5 6 7
funccircularPermutation(n int, start int) []int { ans := make([]int, 1<<n) for i := range ans { ans[i] = (i >> 1) ^ i ^ start } return ans }
[sol2-Python3]
1 2 3 4 5 6
classSolution: defcircularPermutation(self, n: int, start: int) -> List[int]: ans = [0] * (1 << n) for i inrange(1 << n): ans[i] = (i >> 1) ^ i ^ start return ans
[sol2-JavaScript]
1 2 3 4 5 6 7
var circularPermutation = function(n, start) { const ret = []; for (let i = 0; i < 1 << n; i++) { ret.push((i >> 1) ^ i ^ start); } return ret; };