forked from chenyiAlone/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LetterCombinationsofaPhoneNumber.java
51 lines (47 loc) · 1.67 KB
/
LetterCombinationsofaPhoneNumber.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
package medium;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
*
* ClassName: LetterCombinationsofaPhoneNumber
* @author chenyiAlone
* Create Time: 2018/12/08 14:46:28
* Description: No.17 使用队列的出队/入队操作完成字符串的拼接
*/
public class LetterCombinationsofaPhoneNumber {
public List<String> letterCombinations(String digits) {
int len = digits.length();
LinkedList<String> list = new LinkedList<String>();
for (int i = 0; i < len; i++) {
apped(list, digits.charAt(i) - '2');
}
return list;
}
public static List<String> apped(LinkedList<String> list, int n) {
String[][] strs = { {"a", "b", "c"},
{"d", "e", "f"},
{"g", "h", "i"},
{"j", "k", "l"},
{"m", "n", "o"},
{"p", "q", "r", "s"},
{"t", "u", "v"},
{"w", "x", "y", "z"}
};
int len = list.size();
if (len == 0) {
list.addAll((List<String>)Arrays.asList(strs[n]));
} else {
for (int i = 0; i < len; i++) {
String s = list.poll();
for (int j = 0; j < strs[n].length; j++)
list.offer(s + strs[n][j]);
}
}
return list;
}
public static void main(String[] args) {
String digits = "23";
System.out.println(new LetterCombinationsofaPhoneNumber().letterCombinations(digits));
}
}