class Solution:
def __init__(self):
self.path = []
self.res = []
def combinationSum3(self, k, n):
nums = [i+1 for i in range(9)]
self.dfs(nums, n, k)
return self.res
def dfs(self, nums, n , k):
if len(self.path)==k and sum(self.path)==n:
self.res.append(self.path[:])
return
for i in range(len(nums)):
self.path.append(nums[i])
self.dfs(nums[i+1:], n, k)
self.path.pop()
easy~
没写出来。。。。。
class Solution:
def __init__(self):
self.answers: List[str] = []
self.answer: str = ''
self.letter_map = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
def letterCombinations(self, digits: str) -> List[str]:
self.answers.clear()
if not digits: return []
self.backtracking(digits, 0)
return self.answers
def backtracking(self, digits: str, index: int) -> None:
# 回溯函数没有返回值
# Base Case
if index == len(digits): # 当遍历穷尽后的下一层时
self.answers.append(self.answer)
return
# 单层递归逻辑
letters: str = self.letter_map[digits[index]]
for letter in letters:
self.answer += letter # 处理
self.backtracking(digits, index + 1) # 递归至下一层
self.answer = self.answer[:-1] # 回溯