-
Notifications
You must be signed in to change notification settings - Fork 0
/
P078_Subsets.py
36 lines (32 loc) · 952 Bytes
/
P078_Subsets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#coding=utf-8
'''
url: leetcode.com/problems/subsets
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月18日
@details: Solution: 59ms 49.62%
'''
class Solution(object):
def search(self, n, ni, nn, s, si, sn, a):
if si == sn:
a.append(list(s))
return
for i in range(ni, nn):
if i != 0 and n[i]==n[i-1]: continue
s[si] = n[i]
self.search(n, i+1, nn, s, si+1, sn, a)
def subsets(self, n):
"""
:type n: List[int]
:rtype: List[List[int]]
"""
if n == None or len(n) == 0: return []
n.sort(key=None, reverse=False)
a, nn = [], len(n)
for i in range(nn+1):
s = [0] * i
self.search(n, 0, nn, s, 0, i, a)
return a
if __name__ == "__main__":
n = [1 ,2, 3]
print(Solution().subsets(n))