leetcode:17. Letter Combinations of a Phone Number
来源:互联网 发布:好看的搞笑网络短剧 编辑:程序博客网 时间:2024/05/17 22:05
描述
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”].
思路
思路比较简单,就是不断做笛卡尔积。
代码
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> combineVec; if(digits.empty()) return combineVec; static const string letterMap[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; combineVec.push_back(""); int index; for(int i = 0; i < digits.size(); i++){ index = digits[i] - '0'; if(index < 2 || index > 9) continue; vector<string> temp; const string& letterMapValue = letterMap[index]; for(int j = 0; j < combineVec.size();j++){ for(int k = 0; k < letterMapValue.size(); k++){ temp.push_back(combineVec[j] + letterMapValue[k]); } } combineVec=temp; } return combineVec; }};
结果
他山之玉
C++ O(n) solutioin
vector<string> letterCombinations(string digits) { vector<string> result; if(digits.empty()) return vector<string>(); static const vector<string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; result.push_back(""); // add a seed for the initial case for(int i = 0 ; i < digits.size(); ++i) { int num = digits[i]-'0'; if(num < 0 || num > 9) break; const string& candidate = v[num]; if(candidate.empty()) continue; vector<string> tmp; for(int j = 0 ; j < candidate.size() ; ++j) { for(int k = 0 ; k < result.size() ; ++k) { tmp.push_back(result[k] + candidate[j]); } } result.swap(tmp); } return result;}
在第二句的时候,我用的是digits.size()来判断是否为空,结果时间差了2ms。这个地方下次要注意了。
Java O(n) solution
public class Solution { public List<String> letterCombinations(String digits) { List<String> list = new ArrayList<>(); if (digits == null || digits.trim().length() == 0) return list; String[] str = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; char[] ch = digits.trim().toCharArray(); int sum = 1; String [] arr = new String[ch.length]; for (int i = 0; i < ch.length; i++) { arr[i] = str[ch[i] - '0']; } list = new ArrayList<>(sum); return combination(arr, list, 0, ""); } private List<String> combination(String[] s, List<String> list, int index, String result) { if (result.length() == s.length) { list.add(result); return list; } for (int i = index; i < s.length; i++) { for (int j = 0; j < s[i].length(); j++) { combination(s,list, i + 1, result + s[i].substring(j, j + 1)); } } return list; }
这个是利用递归来做的笛卡尔积。
python O(n) solution
class Solution: # @return a list of strings, [s1, s2] def letterCombinations(self, digits): # define a dictionary of the character <-> number projection pad = {'2':'abc', '3':'def', '4':'ghi', '5':'jkl', '6':'mno', '7':'pqrs', '8':'tuv', '9':'wxyz'} # if the input is empty if len(digits) == 0: return [""] # for number string length equals n-1 d0 = digits[:-1] comb0 = self.letterCombinations(d0) # combine the last digit with previous n-1 string d1 = digits[-1] string1 = pad[d1] comb1 = [] # consider every combinations for s in string1: for c in comb0: cs = c+s comb1.append(cs) return comb1
反思
先写出来,再优化
0 0
- 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
- 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
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- Android APP启动过程
- Java中有两种实现多线程的方式以及两种方式之间的区别
- 解决eclipse生成工程doc文档报错问题
- xy问题
- 想练八段锦?扔掉可爱漫画版吧,现在出真人教练版了!
- leetcode:17. Letter Combinations of a Phone Number
- vue 学习之路
- 安卓学习的网站
- 访问action的方法
- 网络
- java后台框架源码 springmvc mybatis HTML5 bootstrap 全新技术
- 禅道架设在自定义的空间上 PHP
- C++ Json 变量的初始化
- java常用的几种线程池比较