7he www.code.he.cn 第13关的密码是什么...

给定一个仅包含数字&2-9&的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与***按键相同)。注意 1 不对应任何字母。
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
说明:尽管上面的***是按字典序排列的,但是你可以任意选择***输出的顺序。
分析 : 回溯法,通过了,效率不高
class Solution {
static String info[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List&String& letterCombinations(String digits) {
List&String& list = new ArrayList&String&();
if(digits.equals("")) return
query(digits, "", list);
private static void query(String s, String res, List&String& list) {
if (s.equals("")) {
list.add(res);
String sp = info[s.charAt(0) - '0'];
for (int j = 0; j & sp.length(); j++) {
query(s.substring(1), res + sp.charAt(j), list);
阅读(...) 评论()

参考资料

 

随机推荐