MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0017/Solution.java:15–59  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/15 desc :

Source from the content-addressed store, hash-verified

13 * </pre>
14 */
15public class Solution {
16// private static String[] map = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
17//
18// public List<String> letterCombinations(String digits) {
19// if (digits.length() == 0) return Collections.emptyList();
20// List<String> list = new ArrayList<>();
21// helper(list, digits, "");
22// return list;
23// }
24//
25// private void helper(List<String> list, String digits, String ans) {
26// if (ans.length() == digits.length()) {
27// list.add(ans);
28// return;
29// }
30// for (char c : map[digits.charAt(ans.length()) - '2'].toCharArray()) {
31// helper(list, digits, ans + c);
32// }
33// }
34
35 private static String[] map = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
36
37 public List<String> letterCombinations(String digits) {
38 if (digits.length() == 0) return Collections.emptyList();
39 LinkedList<String> list = new LinkedList<>();
40 list.add("");
41 char[] charArray = digits.toCharArray();
42 for (int i = 0; i < charArray.length; i++) {
43 char c = charArray[i];
44
45 while (list.getFirst().length() == i) {
46 String pop = list.removeFirst();
47 for (char v : map[c - '2'].toCharArray()) {
48 list.addLast(pop + v);
49 }
50 }
51 }
52 return list;
53 }
54
55 public static void main(String[] args) {
56 Solution solution = new Solution();
57 System.out.println(solution.letterCombinations("23"));
58 }
59}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected