Skip to content

Commit 2113330

Browse files
committed
feat: update leetcode solutions: No.0046. Permutations
1 parent bb4bd30 commit 2113330

File tree

4 files changed

+186
-28
lines changed

4 files changed

+186
-28
lines changed

solution/0000-0099/0046.Permutations/README.md

+94
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,110 @@
3030

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

33+
回溯法:
34+
3335
```python
36+
class Solution:
37+
def permute(self, nums: List[int]) -> List[List[int]]:
38+
def dfs(nums, i, res, path, used):
39+
if i == len(nums):
40+
res.append(copy.deepcopy(path))
41+
return
42+
for j in range(len(nums)):
43+
if not used[j]:
44+
path.append(nums[j])
45+
used[j] = True
46+
dfs(nums, i + 1, res, path, used)
47+
used[j] = False
48+
path.pop()
49+
50+
res, path = [], []
51+
used = [False] * len(nums)
52+
dfs(nums, 0, res, path, used)
53+
return res
54+
```
3455

56+
切分数组:
57+
58+
```python
59+
class Solution:
60+
def permute(self, nums: List[int]) -> List[List[int]]:
61+
if len(nums) <= 1:
62+
return [nums]
63+
res = []
64+
for i, num in enumerate(nums):
65+
n = nums[:i] + nums[i + 1:]
66+
for item in self.permute(n):
67+
res.append([num] + item)
68+
return res
3569
```
3670

3771
### **Java**
3872

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

75+
回溯法:
76+
4177
```java
78+
class Solution {
79+
public List<List<Integer>> permute(int[] nums) {
80+
List<List<Integer>> res = new ArrayList<>();
81+
List<Integer> path = new ArrayList<>();
82+
boolean[] used = new boolean[nums.length];
83+
dfs(nums, 0, res, path, used);
84+
return res;
85+
}
86+
87+
private void dfs(int[] nums, int i, List<List<Integer>> res, List<Integer> path, boolean[] used) {
88+
if (i == nums.length) {
89+
res.add(new ArrayList<>(path));
90+
return;
91+
}
92+
for (int j = 0; j < nums.length; ++j) {
93+
if (!used[j]) {
94+
path.add(nums[j]);
95+
used[j] = true;
96+
dfs(nums, i + 1, res, path, used);
97+
used[j] = false;
98+
path.remove(path.size() - 1);
99+
}
100+
}
101+
}
102+
}
103+
```
42104

105+
- 递归:
106+
107+
```java
108+
class Solution {
109+
public List<List<Integer>> permute(int[] nums) {
110+
List<List<Integer>> res = new ArrayList<>();
111+
permute(res, nums, 0);
112+
return res;
113+
}
114+
115+
private void permute(List<List<Integer>> res, int[] nums, int start) {
116+
if (start == nums.length) {
117+
List<Integer> t = new ArrayList<>();
118+
for (int e : nums) {
119+
t.add(e);
120+
}
121+
res.add(t);
122+
return;
123+
}
124+
for (int i = start; i < nums.length; ++i) {
125+
swap(nums, i, start);
126+
permute(res, nums, start + 1);
127+
swap(nums, i, start);
128+
}
129+
}
130+
131+
private void swap(int[] nums, int i, int j) {
132+
int t = nums[i];
133+
nums[i] = nums[j];
134+
nums[j] = t;
135+
}
136+
}
43137
```
44138

45139
### **...**

solution/0000-0099/0046.Permutations/README_EN.md

+70-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,82 @@
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
def permute(self, nums: List[int]) -> List[List[int]]:
44+
if len(nums) <= 1:
45+
return [nums]
46+
res = []
47+
for i, num in enumerate(nums):
48+
n = nums[:i] + nums[i + 1:]
49+
for item in self.permute(n):
50+
res.append([num] + item)
51+
return res
4352
```
4453

4554
### **Java**
4655

56+
Backtracking:
57+
4758
```java
59+
class Solution {
60+
public List<List<Integer>> permute(int[] nums) {
61+
List<List<Integer>> res = new ArrayList<>();
62+
List<Integer> path = new ArrayList<>();
63+
boolean[] used = new boolean[nums.length];
64+
dfs(nums, 0, res, path, used);
65+
return res;
66+
}
67+
68+
private void dfs(int[] nums, int i, List<List<Integer>> res, List<Integer> path, boolean[] used) {
69+
if (i == nums.length) {
70+
res.add(new ArrayList<>(path));
71+
return;
72+
}
73+
for (int j = 0; j < nums.length; ++j) {
74+
if (!used[j]) {
75+
path.add(nums[j]);
76+
used[j] = true;
77+
dfs(nums, i + 1, res, path, used);
78+
used[j] = false;
79+
path.remove(path.size() - 1);
80+
}
81+
}
82+
}
83+
}
84+
```
4885

86+
- Recursion:
87+
88+
```java
89+
class Solution {
90+
public List<List<Integer>> permute(int[] nums) {
91+
List<List<Integer>> res = new ArrayList<>();
92+
permute(res, nums, 0);
93+
return res;
94+
}
95+
96+
private void permute(List<List<Integer>> res, int[] nums, int start) {
97+
if (start == nums.length) {
98+
List<Integer> t = new ArrayList<>();
99+
for (int e : nums) {
100+
t.add(e);
101+
}
102+
res.add(t);
103+
return;
104+
}
105+
for (int i = start; i < nums.length; ++i) {
106+
swap(nums, i, start);
107+
permute(res, nums, start + 1);
108+
swap(nums, i, start);
109+
}
110+
}
111+
112+
private void swap(int[] nums, int i, int j) {
113+
int t = nums[i];
114+
nums[i] = nums[j];
115+
nums[j] = t;
116+
}
117+
}
49118
```
50119

51120
### **...**

solution/0000-0099/0046.Permutations/Solution.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
class Solution {
22
public List<List<Integer>> permute(int[] nums) {
3-
List<List<Integer>> list = new ArrayList<>();
4-
permute(list, nums, 0);
5-
return list;
3+
List<List<Integer>> res = new ArrayList<>();
4+
permute(res, nums, 0);
5+
return res;
66
}
7-
8-
private void permute(List<List<Integer>> list, int[] nums, int start) {
9-
int end = nums.length - 1;
10-
if (start == end) {
11-
list.add(Arrays.stream(nums).boxed().collect(Collectors.toList()));
7+
8+
private void permute(List<List<Integer>> res, int[] nums, int start) {
9+
if (start == nums.length) {
10+
List<Integer> t = new ArrayList<>();
11+
for (int e : nums) {
12+
t.add(e);
13+
}
14+
res.add(t);
1215
return;
1316
}
14-
15-
for (int i = start; i <= end; ++i) {
17+
for (int i = start; i < nums.length; ++i) {
1618
swap(nums, i, start);
17-
permute(list, nums, start + 1);
19+
permute(res, nums, start + 1);
1820
swap(nums, i, start);
1921
}
20-
21-
2222
}
23-
24-
private static void swap(int[] nums, int i, int j) {
23+
24+
private void swap(int[] nums, int i, int j) {
2525
int t = nums[i];
2626
nums[i] = nums[j];
2727
nums[j] = t;
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
class Solution:
2-
def permute(self, nums):
3-
"""
4-
:type nums: List[int]
5-
:rtype: List[List[int]]
6-
"""
7-
if len(nums) <= 1:
2+
def permute(self, nums: List[int]) -> List[List[int]]:
3+
if len(nums) <= 1:
84
return [nums]
9-
ans = []
5+
res = []
106
for i, num in enumerate(nums):
11-
n = nums[:i] + nums[i+1:]
12-
for y in self.permute(n):
13-
ans.append([num] + y)
14-
return ans
15-
7+
n = nums[:i] + nums[i + 1:]
8+
for item in self.permute(n):
9+
res.append([num] + item)
10+
return res

0 commit comments

Comments
 (0)