目录
学习目标
- 组合总和III
-
电话号码的字母组合
学习内容
组合总和III
216. 组合总和 IIIhttps://leetcode.cn/problems/combination-sum-iii/
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def dfs(i,amount,tmp):
if i>k+1 or amount>n:return
if i==k+1 and amount==n:
res.append(tmp[1:])
return
for j in range(tmp[-1]+1,min(10,n-amount+1)):
tmp.append(j)
dfs(i+1,amount+j,tmp)
tmp.pop()
res = []
dfs(1,0,[0])
return res
电话号码的字母组合
17. 电话号码的字母组合 - 力扣(LeetCode)https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:return []
tmp = ["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
res = []
n = len(digits)
def dfs(i,ans):
if i==n:
res.append(ans)
return
for s in tmp[int(digits[i])]:
dfs(i+1,ans+s)
dfs(0,"")
return res