因为刚考完试不久,时间实在太赶了,就看了下题,思路还不是很清晰,第一题还算是自己做的,第二题就只能看答案了。在此对认真点评我的小伙伴说声抱歉,最后一次一定认真做。
leetcode 017
代码实现
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]
leetcode 046
代码实现
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