Skip to content

Commit 646d61f

Browse files
committed
Add solution 047
1 parent 7c04aef commit 646d61f

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Complete solutions to Leetcode problems, updated daily.
3434
| 019 | [Remove Nth Node From End of List](https://github.com/yanglbme/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List) | `Linked List`, `Two Pointers` |
3535
| 024 | [Swap Nodes in Pairs](https://github.com/yanglbme/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs) | `Linked List` |
3636
| 046 | [Permutations](https://github.com/yanglbme/leetcode/tree/master/solution/046.Permutations) | `Backtracking` |
37+
| 047 | [Permutations II](https://github.com/yanglbme/leetcode/tree/master/solution/047.Permutations%20II) | `Backtracking` |
3738
| 062 | [Unique Paths](https://github.com/yanglbme/leetcode/tree/master/solution/062.Unique%20Paths) | `Array`, `Dynamic Programming` |
3839
| 063 | [Unique Paths II](https://github.com/yanglbme/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
3940
| 082 | [Remove Duplicates from Sorted List II](https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## 全排列 II
2+
### 题目描述
3+
4+
给定一个可包含重复数字的序列,返回所有不重复的全排列。
5+
6+
示例:
7+
```
8+
输入: [1,1,2]
9+
输出:
10+
[
11+
[1,1,2],
12+
[1,2,1],
13+
[2,1,1]
14+
]
15+
```
16+
17+
### 解法
18+
将数组的首元素依次与数组的每个元素交换(两元素不相等才进行交换),对于每一轮交换,对后面的数组进行递归调用。当元素只剩下一个时,添加此时的数组到 list 中。
19+
20+
```java
21+
class Solution {
22+
public List<List<Integer>> permuteUnique(int[] nums) {
23+
List<List<Integer>> list = new ArrayList<>();
24+
permute(list, nums, 0);
25+
return list;
26+
}
27+
28+
private void permute(List<List<Integer>> list, int[] nums, int start) {
29+
int end = nums.length - 1;
30+
if (start == end) {
31+
List<Integer> tmp = new ArrayList<>();
32+
for (int val : nums) {
33+
tmp.add(val);
34+
}
35+
36+
// 已存在,则不添加
37+
if (!list.contains(tmp)) {
38+
list.add(tmp);
39+
return;
40+
}
41+
42+
}
43+
44+
for (int i = start; i <= end; ++i) {
45+
if (i == start) {
46+
permute(list, nums, start + 1);
47+
} else if (nums[i] != nums[start]) {
48+
// i 与 start 不相等,且对应的数组值也不相等,才进行交换
49+
swap(nums, i, start);
50+
permute(list, nums, start + 1);
51+
swap(nums, i, start);
52+
}
53+
54+
}
55+
56+
57+
}
58+
59+
private static void swap(int[] nums, int i, int j) {
60+
int t = nums[i];
61+
nums[i] = nums[j];
62+
nums[j] = t;
63+
}
64+
}
65+
```
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public List<List<Integer>> permuteUnique(int[] nums) {
3+
List<List<Integer>> list = new ArrayList<>();
4+
permute(list, nums, 0);
5+
return list;
6+
}
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<Integer> tmp = new ArrayList<>();
12+
for (int val : nums) {
13+
tmp.add(val);
14+
}
15+
16+
// 已存在,则不添加
17+
if (!list.contains(tmp)) {
18+
list.add(tmp);
19+
return;
20+
}
21+
22+
}
23+
24+
for (int i = start; i <= end; ++i) {
25+
if (i == start) {
26+
permute(list, nums, start + 1);
27+
} else if (nums[i] != nums[start]) {
28+
// i 与 start 不相等,且对应的数组值也不相等,才进行交换
29+
swap(nums, i, start);
30+
permute(list, nums, start + 1);
31+
swap(nums, i, start);
32+
}
33+
34+
}
35+
36+
}
37+
38+
private static void swap(int[] nums, int i, int j) {
39+
int t = nums[i];
40+
nums[i] = nums[j];
41+
nums[j] = t;
42+
}
43+
}

0 commit comments

Comments
 (0)