Skip to content

1238. Circular Permutation in Binary Representation

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  vector<int> circularPermutation(int n, int start) {
    vector<int> ans;

    for (int i = 0; i < 1 << n; ++i)
      ans.push_back(start ^ i ^ i >> 1);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public List<Integer> circularPermutation(int n, int start) {
    List<Integer> ans = new ArrayList<>();

    for (int i = 0; i < 1 << n; ++i)
      ans.add(start ^ i ^ i >> 1);

    return ans;
  }
}
1
2
3
class Solution:
  def circularPermutation(self, n: int, start: int) -> list[int]:
    return [start ^ i ^ i >> 1 for i in range(1 << n)]