标签:return ack int dig end track back jpg 代码实现
刚考完试的人没资格做代码分析。hhh其实是时间太赶了,就看了下题,思路还不是很清晰,第一题还算是自己做的,第二题就只能看答案了。
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
mapping = {‘2‘: ‘abc‘, ‘3‘: ‘def‘, ‘4‘: ‘ghi‘, ‘5‘: ‘jkl‘,
‘6‘: ‘mno‘, ‘7‘: ‘pqrs‘, ‘8‘: ‘tuv‘, ‘9‘: ‘wxyz‘}
if len(digits) == 0:
return []
if len(digits) == 1:
return list(mapping[digits[0]])
prev = self.letterCombinations(digits[:-1])
additional = mapping[digits[-1]]
return [s + c for s in prev for c in additional]
class Solution:
def dfs(self, nums, path, res):
"""
:type nums: List[int]
:type path: int
:type res: List[List[int]]
"""
if not nums:
res.append(path)
# return # backtracking
for i in range(len(nums)):
self.dfs(nums[:i]+nums[i+1:], path+[nums[i]], res)
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.dfs(nums, [], res)
return res
标签:return ack int dig end track back jpg 代码实现
原文地址:https://www.cnblogs.com/ChanWunsam/p/10241748.html