标签:tco 17. 字母 暴力 hash ble || return 电话
暴力即可,深搜 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));
}
}
标签:tco 17. 字母 暴力 hash ble || return 电话
原文地址:https://www.cnblogs.com/acbingo/p/9250468.html