Skip to content

Commit 262b09f

Browse files
committed
feat: add solutions to lc problem: No.0060
No.0060.Permutation Sequence
1 parent 309628c commit 262b09f

File tree

7 files changed

+353
-33
lines changed

7 files changed

+353
-33
lines changed

solution/0000-0099/0060.Permutation Sequence/README.md

+132-1
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,153 @@
5757

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

60+
**方法一:枚举**
61+
62+
我们知道,集合 $[1,2,..n]$ 一共有 $n!$ 种排列,如果我们确定首位,那剩余位能组成的排列数量为 $(n-1)!$。
63+
64+
因此,我们枚举每一位 $i$,如果此时 $k$ 大于当前位置确定后的排列数量,那么我们可以直接减去这个数量;否则,说明我们找到了当前位置的数。
65+
66+
对于每一位 $i$,其中 $0 \leq i \lt n$,剩余位能组成的排列数量为 $(n-i-1)!$,我们记为 $fact$。过程中已使用的数记录在 `vis` 中。
67+
68+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。
69+
6070
<!-- tabs:start -->
6171

6272
### **Python3**
6373

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

6676
```python
67-
77+
class Solution:
78+
def getPermutation(self, n: int, k: int) -> str:
79+
ans = []
80+
vis = [False] * (n + 1)
81+
for i in range(n):
82+
fact = 1
83+
for j in range(1, n - i):
84+
fact *= j
85+
for j in range(1, n + 1):
86+
if not vis[j]:
87+
if k > fact:
88+
k -= fact
89+
else:
90+
ans.append(str(j))
91+
vis[j] = True
92+
break
93+
return ''.join(ans)
6894
```
6995

7096
### **Java**
7197

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

74100
```java
101+
class Solution {
102+
public String getPermutation(int n, int k) {
103+
StringBuilder ans = new StringBuilder();
104+
boolean[] vis = new boolean[n + 1];
105+
for (int i = 0; i < n; ++i) {
106+
int fact = 1;
107+
for (int j = 1; j < n - i; ++j) {
108+
fact *= j;
109+
}
110+
for (int j = 1; j <= n; ++j) {
111+
if (!vis[j]) {
112+
if (k > fact) {
113+
k -= fact;
114+
} else {
115+
ans.append(j);
116+
vis[j] = true;
117+
break;
118+
}
119+
}
120+
}
121+
}
122+
return ans.toString();
123+
}
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
string getPermutation(int n, int k) {
133+
string ans;
134+
bitset<10> vis;
135+
for (int i = 0; i < n; ++i) {
136+
int fact = 1;
137+
for (int j = 1; j < n - i; ++j) fact *= j;
138+
for (int j = 1; j <= n; ++j) {
139+
if (vis[j]) continue;
140+
if (k > fact) k -= fact;
141+
else {
142+
ans += to_string(j);
143+
vis[j] = 1;
144+
break;
145+
}
146+
}
147+
}
148+
return ans;
149+
}
150+
};
151+
```
152+
153+
### **Go**
154+
155+
```go
156+
func getPermutation(n int, k int) string {
157+
ans := make([]byte, n)
158+
vis := make([]bool, n+1)
159+
for i := 0; i < n; i++ {
160+
fact := 1
161+
for j := 1; j < n-i; j++ {
162+
fact *= j
163+
}
164+
for j := 1; j <= n; j++ {
165+
if !vis[j] {
166+
if k > fact {
167+
k -= fact
168+
} else {
169+
ans[i] = byte('0' + j)
170+
vis[j] = true
171+
break
172+
}
173+
}
174+
}
175+
}
176+
return string(ans)
177+
}
178+
```
75179

180+
### **C#**
181+
182+
```cs
183+
public class Solution {
184+
public string GetPermutation(int n, int k) {
185+
var ans = new StringBuilder();
186+
int vis = 0;
187+
for (int i = 0; i < n; ++i) {
188+
int fact = 1;
189+
for (int j = 1; j < n - i; ++j) {
190+
fact *= j;
191+
}
192+
for (int j = 1; j <= n; ++j) {
193+
if (((vis >> j) & 1) == 0) {
194+
if (k > fact) {
195+
k -= fact;
196+
} else {
197+
ans.Append(j);
198+
vis |= 1 << j;
199+
break;
200+
}
201+
}
202+
}
203+
}
204+
return ans.ToString();
205+
}
206+
}
76207
```
77208

78209
### **...**

solution/0000-0099/0060.Permutation Sequence/README_EN.md

+122-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,134 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def getPermutation(self, n: int, k: int) -> str:
50+
ans = []
51+
vis = [False] * (n + 1)
52+
for i in range(n):
53+
fact = 1
54+
for j in range(1, n - i):
55+
fact *= j
56+
for j in range(1, n + 1):
57+
if not vis[j]:
58+
if k > fact:
59+
k -= fact
60+
else:
61+
ans.append(str(j))
62+
vis[j] = True
63+
break
64+
return ''.join(ans)
4965
```
5066

5167
### **Java**
5268

5369
```java
70+
class Solution {
71+
public String getPermutation(int n, int k) {
72+
StringBuilder ans = new StringBuilder();
73+
boolean[] vis = new boolean[n + 1];
74+
for (int i = 0; i < n; ++i) {
75+
int fact = 1;
76+
for (int j = 1; j < n - i; ++j) {
77+
fact *= j;
78+
}
79+
for (int j = 1; j <= n; ++j) {
80+
if (!vis[j]) {
81+
if (k > fact) {
82+
k -= fact;
83+
} else {
84+
ans.append(j);
85+
vis[j] = true;
86+
break;
87+
}
88+
}
89+
}
90+
}
91+
return ans.toString();
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
string getPermutation(int n, int k) {
102+
string ans;
103+
bitset<10> vis;
104+
for (int i = 0; i < n; ++i) {
105+
int fact = 1;
106+
for (int j = 1; j < n - i; ++j) fact *= j;
107+
for (int j = 1; j <= n; ++j) {
108+
if (vis[j]) continue;
109+
if (k > fact) k -= fact;
110+
else {
111+
ans += to_string(j);
112+
vis[j] = 1;
113+
break;
114+
}
115+
}
116+
}
117+
return ans;
118+
}
119+
};
120+
```
121+
122+
### **Go**
123+
124+
```go
125+
func getPermutation(n int, k int) string {
126+
ans := make([]byte, n)
127+
vis := make([]bool, n+1)
128+
for i := 0; i < n; i++ {
129+
fact := 1
130+
for j := 1; j < n-i; j++ {
131+
fact *= j
132+
}
133+
for j := 1; j <= n; j++ {
134+
if !vis[j] {
135+
if k > fact {
136+
k -= fact
137+
} else {
138+
ans[i] = byte('0' + j)
139+
vis[j] = true
140+
break
141+
}
142+
}
143+
}
144+
}
145+
return string(ans)
146+
}
147+
```
54148

149+
### **C#**
150+
151+
```cs
152+
public class Solution {
153+
public string GetPermutation(int n, int k) {
154+
var ans = new StringBuilder();
155+
int vis = 0;
156+
for (int i = 0; i < n; ++i) {
157+
int fact = 1;
158+
for (int j = 1; j < n - i; ++j) {
159+
fact *= j;
160+
}
161+
for (int j = 1; j <= n; ++j) {
162+
if (((vis >> j) & 1) == 0) {
163+
if (k > fact) {
164+
k -= fact;
165+
} else {
166+
ans.Append(j);
167+
vis |= 1 << j;
168+
break;
169+
}
170+
}
171+
}
172+
}
173+
return ans.ToString();
174+
}
175+
}
55176
```
56177

57178
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string getPermutation(int n, int k) {
4+
string ans;
5+
bitset<10> vis;
6+
for (int i = 0; i < n; ++i) {
7+
int fact = 1;
8+
for (int j = 1; j < n - i; ++j) fact *= j;
9+
for (int j = 1; j <= n; ++j) {
10+
if (vis[j]) continue;
11+
if (k > fact) k -= fact;
12+
else {
13+
ans += to_string(j);
14+
vis[j] = 1;
15+
break;
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Text;
4-
51
public class Solution {
62
public string GetPermutation(int n, int k) {
7-
--k;
8-
var factorial = Enumerable.Range(0, n).Select(i => Enumerable.Range(1, i).Aggregate(1, (agg, x) => agg * x)).ToArray();
9-
var numbers = new SortedSet<int>(Enumerable.Range(1, n));
10-
var sb = new StringBuilder();
11-
while (sb.Length < n)
12-
{
13-
var f = factorial[numbers.Count - 1];
14-
var index = k / f;
15-
var number = numbers.ElementAt(index);
16-
sb.Append(number);
17-
numbers.Remove(number);
18-
k %= f;
3+
var ans = new StringBuilder();
4+
int vis = 0;
5+
for (int i = 0; i < n; ++i) {
6+
int fact = 1;
7+
for (int j = 1; j < n - i; ++j) {
8+
fact *= j;
9+
}
10+
for (int j = 1; j <= n; ++j) {
11+
if (((vis >> j) & 1) == 0) {
12+
if (k > fact) {
13+
k -= fact;
14+
} else {
15+
ans.Append(j);
16+
vis |= 1 << j;
17+
break;
18+
}
19+
}
20+
}
1921
}
20-
return sb.ToString();
22+
return ans.ToString();
2123
}
22-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func getPermutation(n int, k int) string {
2+
ans := make([]byte, n)
3+
vis := make([]bool, n+1)
4+
for i := 0; i < n; i++ {
5+
fact := 1
6+
for j := 1; j < n-i; j++ {
7+
fact *= j
8+
}
9+
for j := 1; j <= n; j++ {
10+
if !vis[j] {
11+
if k > fact {
12+
k -= fact
13+
} else {
14+
ans[i] = byte('0' + j)
15+
vis[j] = true
16+
break
17+
}
18+
}
19+
}
20+
}
21+
return string(ans)
22+
}

0 commit comments

Comments
 (0)