Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cd1d2e7

Browse files
authoredOct 19, 2018
Add files via upload
1 parent 5733428 commit cd1d2e7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
 

‎Subsets II/Subsets_II.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 44ms 99.26%
2+
class Solution:
3+
def subsetsWithDup(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: List[List[int]]
7+
"""
8+
res_list = [[]]
9+
nums.sort()
10+
def dfs(nums, temp_list):
11+
for i in range(len(nums)):
12+
if i > 0 and nums[i] == nums[i - 1]:
13+
continue
14+
res_list.append(temp_list + [nums[i]])
15+
dfs(nums[i + 1:], temp_list + [nums[i]])
16+
dfs(nums, [])
17+
return res_list

‎Subsets/Subsets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 48ms 42.54%
2+
class Solution:
3+
def subsets(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: List[List[int]]
7+
"""
8+
def dfs(nums, temp_list):
9+
for index in range(len(nums)):
10+
res_list.append(temp_list + [nums[index]])
11+
dfs(nums[index + 1:], temp_list + [nums[index]])
12+
13+
# 这里因此res_list至少有一个[],所以提前先加好
14+
res_list = [[]]
15+
dfs(nums, [])
16+
return res_list

0 commit comments

Comments
 (0)
Please sign in to comment.