Skip to content

Commit 51b3ff5

Browse files
committed
序列子集(重复元素/无重复元素)
1 parent 82d2c40 commit 51b3ff5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Backtracking/78_Subsets.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding: utf8
2+
3+
4+
"""
5+
题目链接: https://leetcode.com/problems/subsets/description.
6+
题目描述:
7+
8+
Given a set of distinct integers, nums, return all possible subsets (the power set).
9+
10+
Note: The solution set must not contain duplicate subsets.
11+
12+
For example,
13+
If nums = [1,2,3], a solution is:
14+
15+
[
16+
[3],
17+
[1],
18+
[2],
19+
[1,2,3],
20+
[1,3],
21+
[2,3],
22+
[1,2],
23+
[]
24+
]
25+
26+
"""
27+
28+
29+
class Solution(object):
30+
def subsets(self, nums):
31+
"""
32+
:type nums: List[int]
33+
:rtype: List[List[int]]
34+
"""
35+
if not nums:
36+
return []
37+
38+
ans = []
39+
sub = []
40+
self.backtrace(nums, 0, sub, ans)
41+
42+
return ans
43+
44+
def backtrace(self, nums, idx, sub, ans):
45+
ans.append(sub[:])
46+
for i in range(idx, len(nums)):
47+
sub.append(nums[i])
48+
self.backtrace(nums, i + 1, sub, ans)
49+
sub.pop()

Backtracking/90_SubsetsII.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# coding: utf8
2+
3+
4+
"""
5+
题目链接: https://leetcode.com/problems/subsets-ii/description.
6+
题目描述:
7+
8+
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
9+
10+
Note: The solution set must not contain duplicate subsets.
11+
12+
For example,
13+
If nums = [1,2,2], a solution is:
14+
15+
[
16+
[2],
17+
[1],
18+
[1,2,2],
19+
[2,2],
20+
[1,2],
21+
[]
22+
]
23+
24+
"""
25+
26+
27+
class Solution(object):
28+
def subsetsWithDup(self, nums):
29+
"""
30+
:type nums: List[int]
31+
:rtype: List[List[int]]
32+
"""
33+
if not nums:
34+
return []
35+
36+
ans = []
37+
sub = []
38+
visited = [0 for _ in range(len(nums))]
39+
nums.sort()
40+
self.backtrace(nums, 0, visited, sub, ans)
41+
42+
return ans
43+
44+
def backtrace(self, nums, idx, visited, sub, ans):
45+
ans.append(sub[:])
46+
for i in range(idx, len(nums)):
47+
# 前面有重复元素未使用
48+
if i > 0 and nums[i] == nums[i - 1] and not visited[i - 1]:
49+
continue
50+
visited[i] = 1
51+
sub.append(nums[i])
52+
self.backtrace(nums, i + 1, visited, sub, ans)
53+
sub.pop()
54+
visited[i] = 0

ProblemsList.md

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
* 47\. [Permutations II](https://leetcode.com/problems/permutations-ii/description)
197197
* 51\. [N-Queens](https://leetcode.com/problems/n-queens/description)
198198
* 52\. [N-Queens II](https://leetcode.com/problems/n-queens-ii/description)
199+
* 78\. [Subsets](https://leetcode.com/problems/subsets/description)
199200
* 79\. [Word Search](https://leetcode.com/problems/word-search/description)
200201
* 90\. [Subsets II](https://leetcode.com/problems/subsets-ii/description)
201202
* 93\. [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/description)

0 commit comments

Comments
 (0)