216 组合总和 III
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
rets=[]
temp=[]
def sub(temp,inp,cout,sums):
if sums==n and cout==k:
rets.append(temp[:])
return
if sums>n or cout>k:
return
for i in range(inp+1,10):
temp.append(i)
sub(temp,i,cout+1,sums+i)
temp.pop()
for i in range(1,10):
sub([i],i,1,i)
return rets
17
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
phoneMap = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
def sub(index):
if index==len(digits):
rets.append(''.join(ret))
else:
i=digits[index]
for l in phoneMap[i]:
ret.append(l)
sub(index+1)
ret.pop()
ret=[]
rets=[]
sub(0)
return rets