Topic links: https://leetcode-cn.com/problems/subsets-ii/
Subject description:
Given an array of integers may contain duplicate elements of the nums , which returns an array of all possible subsets (power set).
Description: Solution Set can not contain duplicate subsets.
Example:
输入: [1,2,2]
输出:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Ideas:
A thought: Recursive
Thinking two: Iteration
Direct look at the code
Code:
A thought:
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res = []
n = len(nums)
nums.sort()
def helper(idx, tmp):
res.append(tmp)
for i in range(idx, n):
if i > idx and nums[i] == nums[i-1]:
continue
helper(i+1, tmp + [nums[i]])
helper(0, [])
return res
java
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) return res;
Arrays.sort(nums);
backtrack(0, nums, res, new ArrayList<>());
return res;
}
public void backtrack(int idx, int[] nums, List<List<Integer>> res, List<Integer> tmp_list) {
res.add(new ArrayList<>(tmp_list));
for (int i = idx; i < nums.length; i++) {
if (i > idx && nums[i - 1] == nums[i]) continue;
tmp_list.add(nums[i]);
backtrack(i + 1, nums, res, tmp_list);
tmp_list.remove(tmp_list.size() - 1);
}
}
}
Thinking two:
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
if not nums: return []
nums.sort()
res = [[]]
cur = []
for i in range(len(nums)):
if i > 0 and nums[i - 1] == nums[i]:
cur = [tmp + [nums[i]] for tmp in cur]
else:
cur = [tmp + [nums[i]] for tmp in res]
res += cur
return res
Similar topics are:
39. The combination of the sum
Such questions are of the same type, with backtracking algorithm!
In fact, the key lies in backtracking algorithm: it does not, for back
Then constraints, reduced time complexity.
You can find out a little feeling from the following solution!
class Solution:
def subsets(self, nums):
if not nums:
return []
res = []
n = len(nums)
def helper(idx, temp_list):
res.append(temp_list)
for i in range(idx, n):
helper(i + 1, temp_list + [nums[i]])
helper(0, [])
return res
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return []
n = len(nums)
res = []
nums.sort()
# 思路1
def helper1(idx, n, temp_list):
if temp_list not in res:
res.append(temp_list)
for i in range(idx, n):
helper1(i + 1, n, temp_list + [nums[i]])
# 思路2
def helper2(idx, n, temp_list):
res.append(temp_list)
for i in range(idx, n):
if i > idx and nums[i] == nums[i - 1]:
continue
helper2(i + 1, n, temp_list + [nums[i]])
helper2(0, n, [])
return res
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return
res = []
n = len(nums)
visited = [0] * n
def helper1(temp_list,length):
if length == n:
res.append(temp_list)
for i in range(n):
if visited[i] :
continue
visited[i] = 1
helper1(temp_list+[nums[i]],length+1)
visited[i] = 0
def helper2(nums,temp_list,length):
if length == n:
res.append(temp_list)
for i in range(len(nums)):
helper2(nums[:i]+nums[i+1:],temp_list+[nums[i]],length+1)
helper1([],0)
return res
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return []
nums.sort()
n = len(nums)
visited = [0] * n
res = []
def helper1(temp_list, length):
# if length == n and temp_list not in res:
# res.append(temp_list)
if length == n:
res.append(temp_list)
for i in range(n):
if visited[i] or (i > 0 and nums[i] == nums[i - 1] and not visited[i - 1]):
continue
visited[i] = 1
helper1(temp_list + [nums[i]], length + 1)
visited[i] = 0
def helper2(nums, temp_list, length):
if length == n and temp_list not in res:
res.append(temp_list)
for i in range(len(nums)):
helper2(nums[:i] + nums[i + 1:], temp_list + [nums[i]], length + 1)
helper1([],0)
# helper2(nums, [], 0)
return res
39. The combination of the sum
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
if not candidates:
return []
if min(candidates) > target:
return []
candidates.sort()
res = []
def helper(candidates, target, temp_list):
if target == 0:
res.append(temp_list)
if target < 0:
return
for i in range(len(candidates)):
if candidates[i] > target:
break
helper(candidates[i:], target - candidates[i], temp_list + [candidates[i]])
helper(candidates,target,[])
return res
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if not candidates:
return []
candidates.sort()
n = len(candidates)
res = []
def backtrack(i, tmp_sum, tmp_list):
if tmp_sum == target:
res.append(tmp_list)
return
for j in range(i, n):
if tmp_sum + candidates[j] > target : break
if j > i and candidates[j] == candidates[j-1]:continue
backtrack(j + 1, tmp_sum + candidates[j], tmp_list + [candidates[j]])
backtrack(0, 0, [])
return res