-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhone_Number.java
More file actions
52 lines (45 loc) · 1.23 KB
/
Phone_Number.java
File metadata and controls
52 lines (45 loc) · 1.23 KB
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
package algorithmtest;
import java.util.ArrayList;
import java.util.List;
public class Phone_Number {
public List<String> function(String input)
{
String dict[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
List<String> answer = new ArrayList<String>();
String a = "";
int index = Integer.valueOf(input.substring(0, 1));
for(int j = 0;j < dict[index].length();j++) //根据按键所在位置对应字母数递归
{
a = dict[index].substring(j, j+1);
if(input.length()>1)
{
List<String> temp = function(input.substring(1));
for(String t : temp)
{
a += t;
answer.add(a);
a = dict[index].substring(j, j+1); //还原 算不算回溯?
}
}
else
{
answer.add(a);
}
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Phone_Number pn = new Phone_Number();
long startTime = System.currentTimeMillis();//获取当前时间
List<String> temp = pn.function("12");
long endTime = System.currentTimeMillis();
System.out.println(temp.size());
for(String a : temp)
{
System.out.println(a);
}
//doSomeThing(); //要运行的java程序
System.out.println("程序运行时间:"+(endTime-startTime)+"ms");
}
}