題目原型:
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
基本思路:
遞歸求解
public ArrayListletterCombinations(String digits) { ArrayList list = new ArrayList (); char[] number = digits.toCharArray();//存放電話號碼 int len = number.length;//電話號碼長度 String[] phone = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//字母與數字對應表 int[] total = {0,0,3,3,3,3,3,4,3,4};//每個數字上有多少個字母 int[] answer = new int[len];//指示每個數字所對應字母序列的字符位子,如第一個數字是2,如果answer[0] = 0,表示所對應的字符是a即phone[0].charAt[answer[0]]='a' combinatons(list,phone, number, answer, 0, len, total); return list; } void combinatons(ArrayList list,String[] phone,char[] number , int[] answer , int index , int len , int[] total) { if(index==len) { StringBuffer strbuf = new StringBuffer(); for(int i = 0 ;i