17.*的字母组合

题目:给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母
 1     //方法一:简单递归
 2     String[] dict = {"abc", "def", "ghi", "jkl", "mon", "pqrs", "tuv", "wxyz"};
 3     public List<String> letterConbinations(String digits) {
 4         if(digits.isEmpty() || digits == null) {
 5             return new ArrayList<String>();
 6         }
 7         List<String> result = new ArrayList<>();
 8         fun(result, "", digits);
 9         return result;
10     }
11     public void fun(List<String> result, String cur, String digits) {
12         if(cur.length() == digits.length()) {
13             result.add(cur);
14             return;
15         }
16         int index = digits.charAt(cur.length()) - '2';
17         for(int i = 0; i<dict[index].length(); i++) {
18             fun(result, cur + dict[index].charAt(i), digits);
19         }
20     } 
无论有多困难,都坚强的抬头挺胸,人生是一场醒悟,不要昨天,不要明天,只要今天。不一样的你我,不一样的心态,不一样的人生,顺其自然吧
原文地址:https://www.cnblogs.com/xiyangchen/p/10848588.html