-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_17.py
More file actions
42 lines (35 loc) · 951 Bytes
/
leetcode_17.py
File metadata and controls
42 lines (35 loc) · 951 Bytes
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
# coding=utf-8
from typing import List
class Solution:
"""
电话号码对应的字母组合
"""
mapping = [
"abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"
]
def combinations(self, digits: str, idx: int, s: str, result: List[str]):
"""
:param digits:
:param idx:
:param s:
:param result:
:return:
"""
if idx == len(digits):
result.append(s)
return
chars = self.mapping[int(digits[idx]) - 2]
for i in range(len(chars)):
self.combinations(digits, idx + 1, s + chars[i], result)
def letter_combinations_recursive(self, digits: str) -> List[int]:
"""
Time: O(4^n), Space: O(n)
:param nums:
:return:
"""
if not digits:
return []
result = []
self.combinations(digits, 0, "", result)
return result