public List<String> letterCombinations(String digits) {
//递归 枚举
List<String> result = new ArrayList<String>();
if("".equals(digits)||digits.length()==0)
return result;
String[] map = new String[10];
map[0] = "";
map[1] = "";
map[2] = "abc";
map[3] = "def";
map[4] = "ghi";
map[5] = "jkl";
map[6] = "mno";
map[7] = "pqrs";
map[8] = "tuv";
map[9] = "wxyz";
char[] middleTemp = new char[digits.length()];
dfsGetStr(digits, 0, middleTemp, map, result);
return result;
}
private void dfsGetStr(String digits,int index,
char[] middleStr, String[] map, List<String> result) {
if(index == digits.length()) {
result.add(new String(middleStr));
return;
}
char strChar = digits.charAt(index);
for(int i=0; i<map[strChar-'0'].length(); i++) {
middleStr[index] = map[strChar-'0'].charAt(i);
dfsGetStr(digits, index+1, middleStr, map, result);
}
}
17. Letter Combinations of a Phone Number
最新推荐文章于 2020-06-18 06:31:08 发布