Skip to content

Commit 3499906

Browse files
authored
feat: add solutions to lc problem: No.3437 (#4006)
No.3437.Permutations III
1 parent 4a81021 commit 3499906

File tree

9 files changed

+547
-0
lines changed

9 files changed

+547
-0
lines changed

Diff for: solution/3400-3499/3437.Permutations III/README.md

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3437.Permutations%20III/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3437. Permutations III 🔒](https://leetcode.cn/problems/permutations-iii)
10+
11+
[English Version](/solution/3400-3499/3437.Permutations%20III/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>Given an integer <code>n</code>, an <strong>alternating permutation</strong> is a permutation of the first <code>n</code> positive integers such that no <strong>two</strong> adjacent elements are <strong>both</strong> odd or <strong>both</strong> even.</p>
18+
19+
<p>Return <em>all such </em><strong>alternating permutations</strong> sorted in lexicographical order.</p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[1,4,3,2],[2,1,4,3],[2,3,4,1],[3,2,1,4],[3,4,1,2],[4,1,2,3],[4,3,2,1]]</span></p>
28+
</div>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">n = 2</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">[[1,2],[2,1]]</span></p>
36+
</div>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">[[1,2,3],[3,2,1]]</span></p>
44+
</div>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n &lt;= 10</code></li>
51+
</ul>
52+
53+
<!-- description:end -->
54+
55+
## 解法
56+
57+
<!-- solution:start -->
58+
59+
### 方法一:回溯
60+
61+
我们设计一个函数 $\textit{dfs}(i)$,表示当前要填第 $i$ 个位置的数,位置编号从 $0$ 开始。
62+
63+
在 $\textit{dfs}(i)$ 中,如果 $i \geq n$,说明所有位置都已经填完,将当前排列加入答案数组中。
64+
65+
否则,我们枚举当前位置可以填的数 $j$,如果 $j$ 没有被使用过,并且 $j$ 和当前排列的最后一个数不同奇偶性,我们就可以将 $j$ 放在当前位置,继续递归填下一个位置。
66+
67+
时间复杂度 $O(n \times n!)$,空间复杂度 $O(n)$。其中 $n$ 为排列的长度。
68+
69+
<!-- tabs:start -->
70+
71+
#### Python3
72+
73+
```python
74+
class Solution:
75+
def permute(self, n: int) -> List[List[int]]:
76+
def dfs(i: int) -> None:
77+
if i >= n:
78+
ans.append(t[:])
79+
return
80+
for j in range(1, n + 1):
81+
if not vis[j] and (i == 0 or t[-1] % 2 != j % 2):
82+
t.append(j)
83+
vis[j] = True
84+
dfs(i + 1)
85+
vis[j] = False
86+
t.pop()
87+
88+
ans = []
89+
t = []
90+
vis = [False] * (n + 1)
91+
dfs(0)
92+
return ans
93+
```
94+
95+
#### Java
96+
97+
```java
98+
class Solution {
99+
private List<int[]> ans = new ArrayList<>();
100+
private boolean[] vis;
101+
private int[] t;
102+
private int n;
103+
104+
public int[][] permute(int n) {
105+
this.n = n;
106+
t = new int[n];
107+
vis = new boolean[n + 1];
108+
dfs(0);
109+
return ans.toArray(new int[0][]);
110+
}
111+
112+
private void dfs(int i) {
113+
if (i >= n) {
114+
ans.add(t.clone());
115+
return;
116+
}
117+
for (int j = 1; j <= n; ++j) {
118+
if (!vis[j] && (i == 0 || t[i - 1] % 2 != j % 2)) {
119+
vis[j] = true;
120+
t[i] = j;
121+
dfs(i + 1);
122+
vis[j] = false;
123+
}
124+
}
125+
}
126+
}
127+
```
128+
129+
#### C++
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
vector<vector<int>> permute(int n) {
135+
vector<vector<int>> ans;
136+
vector<bool> vis(n);
137+
vector<int> t;
138+
auto dfs = [&](this auto&& dfs, int i) -> void {
139+
if (i >= n) {
140+
ans.push_back(t);
141+
return;
142+
}
143+
for (int j = 1; j <= n; ++j) {
144+
if (!vis[j] && (i == 0 || t[i - 1] % 2 != j % 2)) {
145+
vis[j] = true;
146+
t.push_back(j);
147+
dfs(i + 1);
148+
t.pop_back();
149+
vis[j] = false;
150+
}
151+
}
152+
};
153+
dfs(0);
154+
return ans;
155+
}
156+
};
157+
```
158+
159+
#### Go
160+
161+
```go
162+
func permute(n int) (ans [][]int) {
163+
vis := make([]bool, n+1)
164+
t := make([]int, n)
165+
var dfs func(i int)
166+
dfs = func(i int) {
167+
if i >= n {
168+
ans = append(ans, slices.Clone(t))
169+
return
170+
}
171+
for j := 1; j <= n; j++ {
172+
if !vis[j] && (i == 0 || t[i-1]%2 != j%2) {
173+
vis[j] = true
174+
t[i] = j
175+
dfs(i + 1)
176+
vis[j] = false
177+
}
178+
}
179+
}
180+
dfs(0)
181+
return
182+
}
183+
```
184+
185+
#### TypeScript
186+
187+
```ts
188+
function permute(n: number): number[][] {
189+
const ans: number[][] = [];
190+
const vis: boolean[] = Array(n).fill(false);
191+
const t: number[] = Array(n).fill(0);
192+
const dfs = (i: number) => {
193+
if (i >= n) {
194+
ans.push([...t]);
195+
return;
196+
}
197+
for (let j = 1; j <= n; ++j) {
198+
if (!vis[j] && (i === 0 || t[i - 1] % 2 !== j % 2)) {
199+
vis[j] = true;
200+
t[i] = j;
201+
dfs(i + 1);
202+
vis[j] = false;
203+
}
204+
}
205+
};
206+
dfs(0);
207+
return ans;
208+
}
209+
```
210+
211+
<!-- tabs:end -->
212+
213+
<!-- solution:end -->
214+
215+
<!-- problem:end -->

0 commit comments

Comments
 (0)