@@ -56,13 +56,112 @@ All the adjacent element differ by one bit. Another valid permutation is [3,1,0,
56
56
### ** Python3**
57
57
58
58
``` python
59
+ class Solution :
60
+ def circularPermutation (self , n : int , start : int ) -> List[int ]:
61
+ g = [i ^ (i >> 1 ) for i in range (1 << n)]
62
+ j = g.index(start)
63
+ return g[j:] + g[:j]
64
+ ```
59
65
66
+ ``` python
67
+ class Solution :
68
+ def circularPermutation (self , n : int , start : int ) -> List[int ]:
69
+ return [i ^ (i >> 1 ) ^ start for i in range (1 << n)]
60
70
```
61
71
62
72
### ** Java**
63
73
64
74
``` java
75
+ class Solution {
76
+ public List<Integer > circularPermutation (int n , int start ) {
77
+ int [] g = new int [1 << n];
78
+ int j = 0 ;
79
+ for (int i = 0 ; i < 1 << n; ++ i) {
80
+ g[i] = i ^ (i >> 1 );
81
+ if (g[i] == start) {
82
+ j = i;
83
+ }
84
+ }
85
+ List<Integer > ans = new ArrayList<> ();
86
+ for (int i = j; i < j + (1 << n); ++ i) {
87
+ ans. add(g[i % (1 << n)]);
88
+ }
89
+ return ans;
90
+ }
91
+ }
92
+ ```
93
+
94
+ ``` java
95
+ class Solution {
96
+ public List<Integer > circularPermutation (int n , int start ) {
97
+ List<Integer > ans = new ArrayList<> ();
98
+ for (int i = 0 ; i < 1 << n; ++ i) {
99
+ ans. add(i ^ (i >> 1 ) ^ start);
100
+ }
101
+ return ans;
102
+ }
103
+ }
104
+ ```
105
+
106
+ ### ** C++**
107
+
108
+ ``` cpp
109
+ class Solution {
110
+ public:
111
+ vector<int > circularPermutation(int n, int start) {
112
+ int g[ 1 << n] ;
113
+ int j = 0;
114
+ for (int i = 0; i < 1 << n; ++i) {
115
+ g[ i] = i ^ (i >> 1);
116
+ if (g[ i] == start) {
117
+ j = i;
118
+ }
119
+ }
120
+ vector<int > ans;
121
+ for (int i = j; i < j + (1 << n); ++i) {
122
+ ans.push_back(g[ i % (1 << n)] );
123
+ }
124
+ return ans;
125
+ }
126
+ };
127
+ ```
128
+
129
+ ```cpp
130
+ class Solution {
131
+ public:
132
+ vector<int> circularPermutation(int n, int start) {
133
+ vector<int> ans(1 << n);
134
+ for (int i = 0; i < 1 << n; ++i) {
135
+ ans[i] = i ^ (i >> 1) ^ start;
136
+ }
137
+ return ans;
138
+ }
139
+ };
140
+ ```
141
+
142
+ ### ** Go**
143
+
144
+ ``` go
145
+ func circularPermutation (n int , start int ) []int {
146
+ g := make ([]int , 1 <<n)
147
+ j := 0
148
+ for i := range g {
149
+ g[i] = i ^ (i >> 1 )
150
+ if g[i] == start {
151
+ j = i
152
+ }
153
+ }
154
+ return append (g[j:], g[:j]...)
155
+ }
156
+ ```
65
157
158
+ ``` go
159
+ func circularPermutation (n int , start int ) (ans []int ) {
160
+ for i := 0 ; i < 1 <<n; i++ {
161
+ ans = append (ans, i^(i>>1 )^start)
162
+ }
163
+ return
164
+ }
66
165
```
67
166
68
167
### ** ...**
0 commit comments