鉴于我对DFS的练习实在太少了,突击练了一下。
发现难道是不难,一定要把终止条件,写的很清楚。
public ArrayList<String> letterCombinations(String digits) {
ArrayList<String> result = new ArrayList<String>();
//Stop
if (null == digits || digits.length() == 0){
result.add("");
return result;
}
String[] map = {"abc","def","ghi","jkl","mno","pqrs", "tuv", "wxyz"};
for (String str : letterCombinations(digits.substring(1))) {
for (char c : map[digits.charAt(0) - '0' - 2].toCharArray()){
result.add(c + str);
}
}
return result;
}
No comments:
Post a Comment