Skip to content

Commit 0d1fe24

Browse files
committedNov 12, 2020
feat: subsets
1 parent a00e23d commit 0d1fe24

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎divide.powx-n.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class Solution:
2+
"""
3+
50. Pow(x, n)
4+
https://leetcode-cn.com/problems/powx-n/
5+
实现 pow(x, n) ,即计算 x 的 n 次幂函数。
6+
"""
27
def myPow(self, x: float, n: int) -> float:
38
return self.mul(x, n) if n >=0 else 1.0 / self.mul(x, -n)
49

‎divide.subsets.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
78. 子集
6+
https://leetcode-cn.com/problems/subsets/
7+
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
8+
"""
9+
def subsets(self, nums: List[int]) -> List[List[int]]:
10+
tmp = []
11+
res = []
12+
n = len(nums)
13+
def dfs(cur):
14+
if cur == n:
15+
res.append(tmp[:])
16+
return
17+
tmp.append(nums[cur])
18+
dfs(cur + 1)
19+
tmp.pop()
20+
dfs(cur + 1)
21+
22+
dfs(0)
23+
return res
24+
25+
26+
so = Solution()
27+
print(so.subsets([1,2,3,4]))

0 commit comments

Comments
 (0)
Please sign in to comment.