Leetcode: Letter Combinations of a Phone Number
2014-06-11 02:24
609 查看
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"].
像这种DFS的题目,常见的写法无外乎两种,使用recursion, 或者用Stack。本文采用了Recursion的方式。做完后积累的经验有:像这种在一个ArrayList里面罗列可能的path的题目,recursion的参数一般包括:包含最终结果的集合(ArrayList),input(String),递归层次level(int),某一条具体的path(String)。最后这个参数虽然不是必须,但是如果使用了它,将会使recursion非常好写:所有关于这条路径的添加、删除、修改都可以以这个具体的path为操作对象,并且一旦条件满足,就可以把这个path添加到最终的结果集合里面去,用ArrayList add函数
还有一些细节需要注意:用switch函数的时候,要记得写break,否则所有的case都会执行
public class Solution { public ArrayList<String> letterCombinations(String digits) { ArrayList<String> res = new ArrayList<String>(); getlist(digits, res, "", 0); return res; } public void getlist(String digits, ArrayList<String> res, String single, int level) { if (digits.length() == single.length()) { res.add(single); return; } int c = (int)(digits.charAt(level) - '0'); //get the specific digit at the level unit String temp = possible(c); char[] letterpool = temp.toCharArray(); for (char st : letterpool) { single += st; getlist(digits, res, single, level+1); single = single.substring(0, single.length() - 1); } } public String possible(int c) { String letters = ""; switch (c) { case 2: letters = "abc"; break; case 3: letters = "def"; break; case 4: letters = "ghi"; break; case 5: letters = "jkl"; break; case 6: letters = "mno"; break; case 7: letters = "pqrs"; break; case 8: letters = "tuv"; break; case 9: letters = "wxyz"; break; case 0: letters = " "; break; } return letters; } }
推荐做法:与上类似但是比上面写的好
public class Solution { public List<String> letterCombinations(String digits) { ArrayList<String> res = new ArrayList<String>(); if (digits==null || digits.length()==0) return res; getlist(digits, res, "", 0); return res; } public void getlist(String digits, ArrayList<String> res, String ss, int level) { if (level == digits.length()) { res.add(ss); return; } String cur = interpret(digits.charAt(level)); for (int i=0; i<cur.length(); i++) { char temp = cur.charAt(i); getlist(digits, res, ss+temp, level+1); } } public String interpret(char c) { String result = ""; switch (c) { case '2': result = "abc"; break; case '3': result = "def"; break; case '4': result = "ghi"; break; case '5': result = "jkl"; break; case '6': result = "mno"; break; case '7': result = "pqrs"; break; case '8': result = "tuv"; break; case '9': result = "wxyz"; break; default: result = ""; } return result; } }
另外一种做法,不用DFS和Recursion的想法,用Iteration, 稍微麻烦一点,但是也是一种方法。
public class Solution { public List<String> letterCombinations(String digits) { ArrayList<String> res = new ArrayList<String>(); if (digits == null) return res; if (digits.length() == 0) { res.add(""); return res; } for (int i=0; i<digits.length(); i++) { char c = digits.charAt(i); String cr = interpret(c); ArrayList<String> newres = new ArrayList<String>(); for (int j=0; j<cr.length(); j++) { char newelem = cr.charAt(j); if (res.size() == 0) newres.add(Character.toString(newelem)); else { for (String item : res) { item = item + newelem; newres.add(item); } } } res = newres; } return res; } public String interpret(char c) { String result = ""; switch (c) { case '2': result = "abc"; break; case '3': result = "def"; break; case '4': result = "ghi"; break; case '5': result = "jkl"; break; case '6': result = "mno"; break; case '7': result = "pqrs"; break; case '8': result = "tuv"; break; case '9': result = "wxyz"; break; default: result = ""; } return result; } }
注意15行的语法问题,要将一个char类型改成String类型,用到Character.toString(char st), 另外String的‘+’连接是可以连接一个String以及char的
相关文章推荐
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- LeetCode_Letter Combinations of a Phone Number
- LeetCode Online Judge 题目C# 练习 - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- [Leetcode 47] 17 Letter Combinations of a Phone Number
- LeetCode 17: Letter Combinations of a Phone Number
- [leetcode]Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- LeetCode18:Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- leetcode - Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- Leetcode Letter Combinations of a Phone Number
- leetcode:Letter Combinations of a Phone Number(手机号码的字母组合)