Given a string of numbers only 2-9, it can return all letter combinations indicated.
Given digital map to letters as follows (the same telephone key). Note 1 does not correspond to any alphabet.
Example:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Description:
While the above answers are arranged in order according to the dictionary, but you can choose the order of the answer output.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/letter-combinations-of-a-phone-number
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Keywords: Tree
1, a data initialized to "" as the root node (root node), then each number corresponding to each character to create a new node, resides in the same parent, and the parent node to perform loop node several layers (such as: parent layer 3 nodes, then to perform three cycles, each node has the same node).
2, node data traverse the tree, the root node to a leaf node combination
public class Test {
private Node root = new Node("");
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return result;
}
char[] chars = digits.toCharArray();
List<List<String>> lists = new ArrayList<>();
for (char c : chars) {
lists.add(char2String(c));
}
//生成树
tree(root, lists, 0);
return ergodicTree(root);
}
//用于临时存储路径
private StringBuilder sb = new StringBuilder();
private List<String> res = new ArrayList<>();
/**
* 遍历树
*
* @param tree
* @return
*/
private List<String> ergodicTree(Node tree) {
getCode(tree, tree.c, sb);
return res;
}
private void getCode(Node node, String str, StringBuilder sb) {
StringBuilder stringBuilder = new StringBuilder(sb);
stringBuilder.append(str);
if (node.nodes.size() > 0) {
for (Node n : node.nodes) {
getCode(n, n.c, stringBuilder);
}
} else {
res.add(stringBuilder.toString());
}
}
private void tree(Node node, List<List<String>> lls, int index) {
List<String> cl = lls.get(index);
for (String c : cl) {
node.nodes.add(new Node(c));
}
index++;
if (index < lls.size()) {
for (Node n : node.nodes) {
tree(n, lls, index);
}
}
}
private static List<String> char2String(char c) {
List<String> characters = null;
switch (c) {
case '1':
characters = Arrays.asList("");
break;
case '2':
characters = Arrays.asList("a", "b", "c");
break;
case '3':
characters = Arrays.asList("d", "e", "f");
break;
case '4':
characters = Arrays.asList("g", "h", "i");
break;
case '5':
characters = Arrays.asList("j", "k", "l");
break;
case '6':
characters = Arrays.asList("m", "n", "o");
break;
case '7':
characters = Arrays.asList("p", "q", "r", "s");
break;
case '8':
characters = Arrays.asList("t", "u", "v");
break;
case '9':
characters = Arrays.asList("w", "x", "y", "z");
break;
}
return characters;
}
private class Node {
String c;
List<Node> nodes;
public Node(String c) {
this.c = c;
nodes = new ArrayList<>();
}
}
}