FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaArithmetic/src/LeetCode/LeetCode17.java at master · git-jhy/JavaArithmetic · GitHub
git-jhy
JavaArithmetic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaArithmetic
/
src
/
LeetCode
/
LeetCode17.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (50 loc) · 1.64 KB
Breadcrumbs
JavaArithmetic
/
src
/
LeetCode
/
LeetCode17.java
Copy path
File metadata and controls
68 lines (50 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
LeetCode
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
LeetCode17
{
/// 17. Letter Combinations of a Phone Number
/// https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
/// 时间复杂度: O(2^len(s))
/// 空间复杂度: O(len(s))
private
String
letterMap
[] = {
" "
,
//0
""
,
//1
"abc"
,
//2
"def"
,
//3
"ghi"
,
//4
"jkl"
,
//5
"mno"
,
//6
"pqrs"
,
//7
"tuv"
,
//8
"wxyz"
//9
};
private
ArrayList
<
String
>
res
;
public
List
<
String
>
letterCombinations
(
String
digits
) {
res
=
new
ArrayList
<>();
if
(
digits
.
equals
(
""
))
return
res
;
findCombination
(
digits
,
0
,
""
);
return
res
;
}
// s中保存了此时从digits[0...index-1]翻译得到的一个字母字符串
// 寻找和digits[index]匹配的字母, 获得digits[0...index]翻译得到的解
private
void
findCombination
(
String
digits
,
int
index
,
String
s
) {
if
(
index
==
digits
.
length
()) {
res
.
add
(
s
);
return
;
}
Character
c
=
digits
.
charAt
(
index
);
String
letters
=
letterMap
[
c
-
'0'
];
for
(
int
i
=
0
;
i
<
letters
.
length
();
i
++) {
findCombination
(
digits
,
index
+
1
,
s
+
letters
.
charAt
(
i
));
}
return
;
}
private
static
void
printList
(
List
<
String
>
list
) {
for
(
String
s
:
list
)
System
.
out
.
println
(
s
);
}
public
static
void
main
(
String
[]
args
) {
printList
((
new
LeetCode17
()).
letterCombinations
(
"234"
));
}
}
Back
|
FazBrowse Home
|
New Git URL