leetcode 140: Letter Combinations of a Phone Number
来源:互联网 发布:悬疑电视剧 知乎 编辑:程序博客网 时间:2024/05/17 03:35
1,
Letter Combinations of a Phone Number
Total Accepted: 10538 Total Submissions: 40738Given 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.
2, recursive
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> res; string temp; rec(res, temp, digits, 0); return res; } private: void rec(vector<string> &res, string &temp, const string &digits, unsigned int level) { if(level == digits.length()) { res.push_back(temp); return; } string str = getMapChars(digits[level]); for(int i=0; i< str.length(); i++) { temp.push_back( str[i] ); rec(res, temp, digits, level+1); temp.pop_back(); } } string getMapChars(char digit) { if(digit<'2' || digit >'9') return ""; string arr[] = {"abc","def","ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; return arr[digit - '0' - 2]; }};
2. iterative java
public class Solution { public List<String> letterCombinations(String digits) { //init check List<String> res = new ArrayList<String>(); if(digits==null || digits.length()==0) return res; res.add(""); String[] map = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; for(int i=0; i<digits.length(); i++) { int index = digits.charAt(i) - '2' ; String s = map[index]; //check valid number int len = res.size(); for(int j=0; j<len; j++) { String str = res.get(j); res.set(j, str + s.charAt(0)); for(int m=1; m<s.length(); m++) { res.add(str + s.charAt(m)); } } } return res; }}
3, iterative c++
class Solution {public: vector<string> letterCombinations(string digits) { // Start typing your C/C++ solution below // DO NOT write int main() function const string letters[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> ret(1, ""); for (int i = 0; i < digits.size(); ++i) { for (int j = ret.size() - 1; j >= 0; --j) { const string &s = letters[digits[i] - '2']; for (int k = s.size() - 1; k >= 0; --k) { if (k) ret.push_back(ret[j] + s[k]); else ret[j] += s[k]; } } } return ret; }};
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
- swift 笔记 (九) —— 类和结构体
- 服务器新监控(win + .net)
- Get date without time in C#
- Linux的进程/线程间通信方式总结
- 第六周__利用哈夫曼编码英文字母表
- leetcode 140: Letter Combinations of a Phone Number
- 单目运算符重载为成员函数 前++ 和后++的使用
- 解决NetworkOnMainThreadException
- 反编译Apk得到Java源代码
- Kali Linux 更换 主题
- Remove the default value from numeric textbox in MVC4
- 第七周__背包问题
- Mnesia数据库及其集群的配置
- fsjkfps[l'fs\\,;sa,f'wls;f'slcvsdpof0-pdsfsd;lkfk;s