forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
17_电话号码的字母组合.py
78 lines (69 loc) · 2.49 KB
/
17_电话号码的字母组合.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 09:29:04 2020
@author: leiya
"""
'''
0705
总结:
回溯问题最关键的一点在于明白每一层的解空间是什么,每深入一层,各个层次间的解空间如何变化,这种变化是需要随着递归函数不断传入的,
以此来告诉代码每一层的找解的条件是什么,在什么空间里找,这道题不需要marked,visited这种空间来标记用过的解,因为每一层他的解空间完全不同,
因此上一层的解空间跟下一层互斥不相容,不需要visited来避免找到上一层访问过的解
0710
'''
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
adict= {
'2':['a','b','c'],
'3':['d','e','f'],
'4':['g','h','i'],
'5':['j','k','l'],
'6':['m','n','o'],
'7':['p','q','r','s'],
'8':['t','u','v'],
'9':['w','x','y','z']
}
def dfs(start_index,adict,path,res):
if start_index == len(digits):
res.append(path[:])
return
for num in adict[digits[start_index]]:
path += num
dfs(start_index+1,adict,path,res)
path = path[:-1]
res = []
path = ''
start_index = 0
dfs(start_index,adict,path,res)
return res
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
#注意hashtable中的key最好是字符串类型
phone= {
'2':['a','b','c'],
'3':['d','e','f'],
'4':['g','h','i'],
'5':['j','k','l'],
'6':['m','n','o'],
'7':['p','q','r','s'],
'8':['t','u','v'],
'9':['w','x','y','z']
}
def dfs(start_index,digits,path,res):
if start_index == len(digits):
res.append(path[:])
return
for j in range(len(phone[digits[start_index]])):
path += phone[digits[start_index]][j]
dfs(start_index+1,digits,path,res)
#这里对字符串进行删除时,不能用path - phone[digits[start_index]][j],因为字符串不支持-操作,只能切片
path = path[:-1]
path = ''
res = []
start_index = 0
dfs(start_index,digits,path,res)
return res