LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
来源:互联网 发布:证券编程工作 编辑:程序博客网 时间:2024/05/16 08:11
题目:
解法:
Simple and efficient iterative solution.
Explanation with sample input "123"
Initial state:
- result = {""}
Stage 1 for number "1":
- result has {""}
- candiate is "abc"
- generate three strings "" + "a", ""+"b", ""+"c" and put into tmp,
tmp = {"a", "b","c"} - swap result and tmp (swap does not take memory copy)
- Now result has {"a", "b", "c"}
Stage 2 for number "2":
- result has {"a", "b", "c"}
- candidate is "def"
- generate nine strings and put into tmp,
"a" + "d", "a"+"e", "a"+"f",
"b" + "d", "b"+"e", "b"+"f",
"c" + "d", "c"+"e", "c"+"f" - so tmp has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
- swap result and tmp
- Now result has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
Stage 3 for number "3":
- result has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
- candidate is "ghi"
- generate 27 strings and put into tmp,
- add "g" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
- add "h" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
- add "h" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
- so, tmp has
{"adg", "aeg", "afg", "bdg", "beg", "bfg", "cdg", "ceg", "cfg"
"adh", "aeh", "afh", "bdh", "beh", "bfh", "cdh", "ceh", "cfh"
"adi", "aei", "afi", "bdi", "bei", "bfi", "cdi", "cei", "cfi" } - swap result and tmp
- Now result has
{"adg", "aeg", "afg", "bdg", "beg", "bfg", "cdg", "ceg", "cfg"
"adh", "aeh", "afh", "bdh", "beh", "bfh", "cdh", "ceh", "cfh"
"adi", "aei", "afi", "bdi", "bei", "bfi", "cdi", "cei", "cfi" }
Finally, return result.
class Solution {public: 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; }};
阅读全文
0 0
- LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
- leetcode——Letter Combinations of a Phone Number 手机按键字母组合(AC)
- [LeetCode17]Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17---Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17:Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17. Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number 电话按键的组合
- LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
- 425.Letter Combinations of a Phone Number-电话号码的字母组合(中等题)
- 17:Letter Combinations of a Phone Number(字母组合的电话号码)
- 17. Letter Combinations of a Phone Number(根据手机按键求字母的组合)
- LeetCode17 Letter Combinations of a Phone Number(Python and Java)
- 关于C#上传文件出现UnauthorizedAccessException解决办法
- 前后台$.post交互并返回JSON对象
- tcp_server的实现
- python学习日记:图像模糊
- git push的时候每次都要输入用户名和密码的问题解决
- LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
- warning D9025: 正在重写“/D_DEBUG”(用“/U_DEBUG”)问题
- 【swift】TablviewController reloadData 的问题
- 查看虚拟机里的Centos7的IP
- Java基础教程33-ArrayList
- 【java知识点异常】e.getMessage()与e.printStackTrace()的区别
- tcpdump抓包
- hibernate多对多映射
- 我的学习之路_第十五章_