Skip to content

Commit 4bf73c4

Browse files
committed
feat: add solutions to lc problems: No.0089,1238
* No.0089.Gray Code * No.1238.Circular Permutation in Binary Representation
1 parent e139604 commit 4bf73c4

File tree

11 files changed

+263
-16
lines changed

11 files changed

+263
-16
lines changed

solution/0000-0099/0089.Gray Code/README.md

+25-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,25 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60-
`G(i) = i ^ (i/2)`
60+
**方法一:二进制码转格雷码**
61+
62+
格雷码是我们在工程中常会遇到的一种编码方式,它的基本的特点就是任意两个相邻的代码只有一位二进制数不同。
63+
64+
二进制码转换成二进制格雷码,其法则是保留二进制码的最高位作为格雷码的最高位,而次高位格雷码为二进制码的高位与次高位相异或,而格雷码其余各位与次高位的求法相类似。
65+
66+
假设某个二进制数表示为 $B_{n-1}B_{n-2}...B_2B_1B_0$,其格雷码表示为 $G_{n-1}G_{n-2}...G_2G_1G_0$。最高位保留,所以 $G_{n-1} = B_{n-1}$;而其它各位 $G_i = B_{i+1} \oplus B_{i}$,其中 $i=0,1,2..,n-2$。
67+
68+
因此,对于一个整数 $x$,我们可以用函数 $gray(x)$ 得到其格雷码:
69+
70+
```java
71+
int gray(x) {
72+
return x ^ (x >> 1);
73+
}
74+
```
75+
76+
我们直接将 $[0,..2^n - 1]$ 这些整数映射成对应的格雷码,即可得到答案数组。
77+
78+
时间复杂度 $O(2^n)$,其中 $n$ 为题目给定的整数。忽略答案的空间消耗,空间复杂度 $O(1)$。
6179

6280
<!-- tabs:start -->
6381

@@ -94,7 +112,9 @@ class Solution {
94112
public:
95113
vector<int> grayCode(int n) {
96114
vector<int> ans;
97-
for (int i = 0; i < 1 << n; ++i) ans.push_back(i ^ (i >> 1));
115+
for (int i = 0; i < 1 << n; ++i) {
116+
ans.push_back(i ^ (i >> 1));
117+
}
98118
return ans;
99119
}
100120
};
@@ -103,12 +123,11 @@ public:
103123
### **Go**
104124
105125
```go
106-
func grayCode(n int) []int {
107-
var ans []int
126+
func grayCode(n int) (ans []int) {
108127
for i := 0; i < 1<<n; i++ {
109128
ans = append(ans, i^(i>>1))
110129
}
111-
return ans
130+
return
112131
}
113132
```
114133

@@ -120,7 +139,7 @@ func grayCode(n int) []int {
120139
* @return {number[]}
121140
*/
122141
var grayCode = function (n) {
123-
let ans = [];
142+
const ans = [];
124143
for (let i = 0; i < 1 << n; ++i) {
125144
ans.push(i ^ (i >> 1));
126145
}

solution/0000-0099/0089.Gray Code/README_EN.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ class Solution {
8484
public:
8585
vector<int> grayCode(int n) {
8686
vector<int> ans;
87-
for (int i = 0; i < 1 << n; ++i) ans.push_back(i ^ (i >> 1));
87+
for (int i = 0; i < 1 << n; ++i) {
88+
ans.push_back(i ^ (i >> 1));
89+
}
8890
return ans;
8991
}
9092
};
@@ -93,12 +95,11 @@ public:
9395
### **Go**
9496
9597
```go
96-
func grayCode(n int) []int {
97-
var ans []int
98+
func grayCode(n int) (ans []int) {
9899
for i := 0; i < 1<<n; i++ {
99100
ans = append(ans, i^(i>>1))
100101
}
101-
return ans
102+
return
102103
}
103104
```
104105

@@ -110,7 +111,7 @@ func grayCode(n int) []int {
110111
* @return {number[]}
111112
*/
112113
var grayCode = function (n) {
113-
let ans = [];
114+
const ans = [];
114115
for (let i = 0; i < 1 << n; ++i) {
115116
ans.push(i ^ (i >> 1));
116117
}

solution/0000-0099/0089.Gray Code/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
vector<int> grayCode(int n) {
44
vector<int> ans;
5-
for (int i = 0; i < 1 << n; ++i) ans.push_back(i ^ (i >> 1));
5+
for (int i = 0; i < 1 << n; ++i) {
6+
ans.push_back(i ^ (i >> 1));
7+
}
68
return ans;
79
}
810
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
func grayCode(n int) []int {
2-
var ans []int
1+
func grayCode(n int) (ans []int) {
32
for i := 0; i < 1<<n; i++ {
43
ans = append(ans, i^(i>>1))
54
}
6-
return ans
5+
return
76
}

solution/0000-0099/0089.Gray Code/Solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @return {number[]}
44
*/
55
var grayCode = function (n) {
6-
let ans = [];
6+
const ans = [];
77
for (let i = 0; i < 1 << n; ++i) {
88
ans.push(i ^ (i >> 1));
99
}

solution/1200-1299/1238.Circular Permutation in Binary Representation/README.md

+99
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,114 @@
5151
<!-- 这里可写当前语言的特殊实现逻辑 -->
5252

5353
```python
54+
class Solution:
55+
def circularPermutation(self, n: int, start: int) -> List[int]:
56+
g = [i ^ (i >> 1) for i in range(1 << n)]
57+
j = g.index(start)
58+
return g[j:] + g[:j]
59+
```
5460

61+
```python
62+
class Solution:
63+
def circularPermutation(self, n: int, start: int) -> List[int]:
64+
return [i ^ (i >> 1) ^ start for i in range(1 << n)]
5565
```
5666

5767
### **Java**
5868

5969
<!-- 这里可写当前语言的特殊实现逻辑 -->
6070

6171
```java
72+
class Solution {
73+
public List<Integer> circularPermutation(int n, int start) {
74+
int[] g = new int[1 << n];
75+
int j = 0;
76+
for (int i = 0; i < 1 << n; ++i) {
77+
g[i] = i ^ (i >> 1);
78+
if (g[i] == start) {
79+
j = i;
80+
}
81+
}
82+
List<Integer> ans = new ArrayList<>();
83+
for (int i = j; i < j + (1 << n); ++i) {
84+
ans.add(g[i % (1 << n)]);
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
```java
92+
class Solution {
93+
public List<Integer> circularPermutation(int n, int start) {
94+
List<Integer> ans = new ArrayList<>();
95+
for (int i = 0; i < 1 << n; ++i) {
96+
ans.add(i ^ (i >> 1) ^ start);
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<int> circularPermutation(int n, int start) {
109+
int g[1 << n];
110+
int j = 0;
111+
for (int i = 0; i < 1 << n; ++i) {
112+
g[i] = i ^ (i >> 1);
113+
if (g[i] == start) {
114+
j = i;
115+
}
116+
}
117+
vector<int> ans;
118+
for (int i = j; i < j + (1 << n); ++i) {
119+
ans.push_back(g[i % (1 << n)]);
120+
}
121+
return ans;
122+
}
123+
};
124+
```
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
vector<int> circularPermutation(int n, int start) {
130+
vector<int> ans(1 << n);
131+
for (int i = 0; i < 1 << n; ++i) {
132+
ans[i] = i ^ (i >> 1) ^ start;
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func circularPermutation(n int, start int) []int {
143+
g := make([]int, 1<<n)
144+
j := 0
145+
for i := range g {
146+
g[i] = i ^ (i >> 1)
147+
if g[i] == start {
148+
j = i
149+
}
150+
}
151+
return append(g[j:], g[:j]...)
152+
}
153+
```
62154

155+
```go
156+
func circularPermutation(n int, start int) (ans []int) {
157+
for i := 0; i < 1<<n; i++ {
158+
ans = append(ans, i^(i>>1)^start)
159+
}
160+
return
161+
}
63162
```
64163

65164
### **...**

solution/1200-1299/1238.Circular Permutation in Binary Representation/README_EN.md

+99
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,112 @@ All the adjacent element differ by one bit. Another valid permutation is [3,1,0,
5656
### **Python3**
5757

5858
```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+
```
5965

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)]
6070
```
6171

6272
### **Java**
6373

6474
```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+
```
65157

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+
}
66165
```
67166

68167
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
vector<int> circularPermutation(int n, int start) {
4+
vector<int> ans(1 << n);
5+
for (int i = 0; i < 1 << n; ++i) {
6+
ans[i] = i ^ (i >> 1) ^ start;
7+
}
8+
return ans;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func circularPermutation(n int, start int) (ans []int) {
2+
for i := 0; i < 1<<n; i++ {
3+
ans = append(ans, i^(i>>1)^start)
4+
}
5+
return
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public List<Integer> circularPermutation(int n, int start) {
3+
List<Integer> ans = new ArrayList<>();
4+
for (int i = 0; i < 1 << n; ++i) {
5+
ans.add(i ^ (i >> 1) ^ start);
6+
}
7+
return ans;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def circularPermutation(self, n: int, start: int) -> List[int]:
3+
return [i ^ (i >> 1) ^ start for i in range(1 << n)]

0 commit comments

Comments
 (0)