-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode017.java
57 lines (49 loc) · 1.2 KB
/
Leetcode017.java
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
package test;
import java.util.*;
//无从下手,参考网上递归方法
public class Leetcode017 {
//用于存储答案
List<String> ans = new ArrayList<String>();
//选择的种类,也可以数组实现
public String change(char a) {
switch (a) {
case '0': return " ";
case '2': return "abc";
case '3': return "def";
case '4': return "ghi";
case '5': return "jkl";
case '6': return "mno";
case '7': return "pqrs";
case '8': return "tuv";
case '9': return "wxyz";
}
return "";
}
//递归式
void formString(String digits, String temp) {
if (digits.length() == 0)
ans.add(temp);
else {
for (int i=0; i<change(digits.charAt(0)).length(); i++) {
temp = temp + change(digits.charAt(0)).charAt(i);
formString(digits.substring(1, digits.length()), temp);
temp = temp.substring(0, temp.length()-1);
}
}
}
//清空ans并调用递归式
public List<String> letterCombinations(String digits) {
ans.clear();
if (digits.length() == 0)
return ans;
String temp = "";
formString(digits, temp);
return ans;
}
//主函数
public static void main(String args[]) {
String digits = "23";
Leetcode017 test = new Leetcode017();
System.out.println(test.letterCombinations(digits));
}
}