纯暴力解法,digits有多长,就循环多少次进行字母组合
class Solution {public List<String> letterCombinations(String digits) {List<String> reslut = new ArrayList<>();if(digits.equals(""))return reslut;Map<Integer, String> letter_hash = new HashMap<Integer, String>() {{put(2, "abc");put(3, "def");put(4, "ghi");put(5, "jkl");put(6, "mno");put(7, "pqrs");put(8, "tuv");put(9, "wxyz");}};if(digits.length() == 1){for(char c : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(0)))).toCharArray()){reslut.add(String.valueOf(c));}}if(digits.length() == 2){for(char c : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(0)))).toCharArray()){StringBuilder re =new StringBuilder();for(char d : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(1)))).toCharArray()){re.append(String.valueOf(c));re.append(String.valueOf(d));reslut.add(re.toString());re.delete(0,re.length());}}}if(digits.length() == 3){for(char c : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(0)))).toCharArray()){StringBuilder re =new StringBuilder();for(char d : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(1)))).toCharArray()){for(char e : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(2)))).toCharArray()){re.append(String.valueOf(c));re.append(String.valueOf(d));re.append(String.valueOf(e));reslut.add(re.toString());re.delete(0,re.length());}}}}if(digits.length() == 4){for(char c : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(0)))).toCharArray()){StringBuilder re =new StringBuilder();for(char d : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(1)))).toCharArray()){for(char e : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(2)))).toCharArray()){for(char f : letter_hash.get(Integer.parseInt(String.valueOf(digits.charAt(3)))).toCharArray()){re.append(String.valueOf(c));re.append(String.valueOf(d));re.append(String.valueOf(e));re.append(String.valueOf(f));reslut.add(re.toString());re.delete(0,re.length());}}}}}return reslut;}
}