Skip to content

Commit 5a1879e

Browse files
committed
solve problem Permutations II
1 parent 0605a07 commit 5a1879e

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ All solutions will be accepted!
225225
|40|[Combination Sum II](https://leetcode-cn.com/problems/combination-sum-ii/description/)|[java/py/js](./algorithms/CombinationSumII)|Medium|
226226
|216|[Combination Sum III](https://leetcode-cn.com/problems/combination-sum-iii/description/)|[java/py/js](./algorithms/CombinationSumIII)|Medium|
227227
|46|[Permutations](https://leetcode-cn.com/problems/permutations/description/)|[java/py/js](./algorithms/Permutations)|Medium|
228+
|47|[Permutations II](https://leetcode-cn.com/problems/permutations-ii/description/)|[java/py/js](./algorithms/PermutationsII)|Medium|
228229
|77|[Combinations](https://leetcode-cn.com/problems/combinations/description/)|[java/py/js](./algorithms/Combinations)|Medium|
229230

230231
# Database

algorithms/PermutationsII/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Permutations II
2+
We can solve this proble by Backtracking Algorithm like [Combination Sum II](./CombinationSumII)
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public List<List<Integer>> permuteUnique(int[] nums) {
3+
List<List<Integer>> res = new ArrayList<List<Integer>>();
4+
Arrays.sort(nums);
5+
backTrack(nums, new ArrayList<Integer>(), res);
6+
return res;
7+
}
8+
9+
public void backTrack(int[] nums, List<Integer> temp, List<List<Integer>> res) {
10+
int length = nums.length;
11+
if (length == 0) {
12+
res.add(temp);
13+
} else {
14+
int i = 0;
15+
while (i < length) {
16+
int num = nums[i];
17+
int[] tempNums = new int[length - 1];
18+
System.arraycopy(nums, 0, tempNums, 0, i);
19+
System.arraycopy(nums, i + 1, tempNums, i, length - 1 - i);
20+
List<Integer> t = new ArrayList<Integer>(temp);
21+
t.add(num);
22+
23+
backTrack(tempNums, t, res);
24+
while (i + 1 < length && nums[i + 1] == num) {
25+
i++;
26+
}
27+
i++;
28+
}
29+
}
30+
}
31+
}

algorithms/PermutationsII/solution.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var permuteUnique = function(nums) {
6+
let res = []
7+
nums.sort()
8+
backTrack(nums, [], res)
9+
return res
10+
};
11+
12+
var backTrack = function(nums, temp, res) {
13+
if (nums.length === 0) {
14+
res.push(temp)
15+
} else {
16+
let i = 0
17+
while (i < nums.length) {
18+
let num = nums[i],
19+
tempNums = nums.slice(),
20+
t = temp.slice()
21+
22+
tempNums.splice(i, 1)
23+
t.push(num)
24+
backTrack(tempNums, t, res)
25+
26+
while (i + 1 < nums.length && nums[i + 1] === num) {
27+
i++
28+
}
29+
i++
30+
}
31+
}
32+
}

algorithms/PermutationsII/solution.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def permuteUnique(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[List[int]]
6+
"""
7+
res = []
8+
nums.sort()
9+
self.backtrack(nums, [], res)
10+
return res
11+
12+
def backtrack(self, nums, temp, res):
13+
if len(nums) == 0:
14+
res.append(temp)
15+
else:
16+
i = 0
17+
while i < len(nums):
18+
num = nums[i]
19+
temp_nums = nums[:]
20+
temp_nums.remove(num)
21+
t = temp[:]
22+
t.append(num)
23+
self.backtrack(temp_nums, t, res)
24+
25+
while i + 1 < len(nums) and nums[i + 1] == num:
26+
i += 1
27+
i += 1

0 commit comments

Comments
 (0)