[leetcode] 17. 电话号码的字母组合
暴力即可,深搜 or 迭代
class Solution {
Map<Character, String[]> map = new HashMap<Character, String[]>();
public List<String> letterCombinations(String digits) {
if (null == digits || digits.length() == 0) return new LinkedList<>();
map.put('2', new String[]{"a", "b", "c"});
map.put('3', new String[]{"d", "e", "f"});
map.put('4', new String[]{"g", "h", "i"});
map.put('5', new String[]{"j", "k", "l"});
map.put('6', new String[]{"m", "n", "o"});
map.put('7', new String[]{"p", "q", "r", "s"});
map.put('8', new String[]{"t", "u", "v"});
map.put('9', new String[]{"w", "x", "y", "z"});
List<String> ans = new ArrayList<>();
ans.add("");
ans = dfs(ans, digits);
ans.remove("");
return ans;
}
public List<String> dfs(List<String> list, String digits) {
if (digits.length() == 0) return list;
char ch = digits.charAt(0);
List<String> newList = new ArrayList<>();
String[] strings = map.get(ch);
for (String c : strings) {
for (String s : list) {
newList.add(s + c);
}
}
return dfs(newList, digits.substring(1));
}
}