Skip to content

Commit c6e26cc

Browse files
committed
9宫格手机键盘组合字符串(dfs)
1 parent dd195d5 commit c6e26cc

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

String/17_LetterCombinationsOfAPhoneNumber.py renamed to Backtracking/17_LetterCombinationsOfAPhoneNumber.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@ def letterCombinations(self, digits):
2323
:type digits: str
2424
:rtype: List[str]
2525
"""
26-
pass
26+
if not digits:
27+
return []
28+
29+
dtm = {'0': '', '1': '', '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv',
30+
'9': 'wxyz'}
31+
ans = []
32+
self.backtrack(digits, dtm, 0, '', ans)
33+
34+
return ans
35+
36+
def backtrack(self, digits, dtm, idx, s, ans):
37+
if len(s) == len(digits):
38+
ans.append(s)
39+
else:
40+
for i in range(idx, len(digits)):
41+
for c in dtm[digits[i]]:
42+
self.backtrack(digits, dtm, i + 1, s + c, ans)

0 commit comments

Comments
 (0)