回溯法 | 电话号码的字符组合
BUGGG !!!
/* 字符转数组:ineteger.valueof(string s) 不适用integer.valueof(char ch) 适用 ch-'0'&nbs***bsp;Integer.valueof(s.substring(i,i+1)) */
class Solution { List<String> s = new ArrayList<>(); public List<String> letterCombinations(String digits) { if(digits.length()==0) return s; HashMap<Integer,String> map = new HashMap<>(); map.put(1,""); map.put(2,"abc"); map.put(3,"def"); map.put(4,"ghi"); map.put(5,"jkl"); map.put(6,"mno"); map.put(7,"pqrs"); map.put(8,"tuv"); map.put(9,"wxyz"); dfs(map,digits,"",0); return s; } public void dfs(HashMap<Integer,String> map, String digits, String ss, int idx){ if(idx==digits.length()){ s.add(ss); return; } // Bug /* 字符转数组:ineteger.valueof(string s) 不适用integer.valueof(char ch) 适用 ch-'0'&nbs***bsp;Integer.valueof(s.substring(i,i+1)) */ String st = map.get(digits.charAt(idx)-'0'); for(int i=0;i<st.length();i++){ ss = ss + st.substring(i,i+1); dfs(map,digits,ss,idx+1); ss = ss.substring(0,ss.length()-1); } } }