Letter Combinations of a Phone Number @LeetCode
package Level3; import java.util.ArrayList; /** * Letter Combinations of a Phone Number * * 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. http://upload.wikimedia.org/易做图/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png 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 class S17 { public static void main(String[] args) { System.out.println(letterCombinations("23")); } public static ArrayList<String> letterCombinations(String digits) { String[] ss = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; ArrayList<String> ret = new ArrayList<String>(); rec(ret, digits.length(), ss, digits, new StringBuffer()); return ret; } public static void rec(ArrayList<String> ret, int remain, String[] ss, String digits, StringBuffer sb){ // 说明对digits遍历结束 if(remain == 0){ ret.add(sb.toString()); return; } String s = ss[digits.charAt(0)-'0']; // 得到digits[0]对应的string s // DFS for(int i=0; i<s.length(); i++){ sb = sb.append(s.charAt(i)); rec(ret, remain-1, ss, digits.substring(1), sb); sb.deleteCharAt(sb.length()-1); // 恢复现场 } } }
补充:软件开发 , Java ,