LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)
题目:
解法:
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 casefor(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;}
};
LeetCode17——Letter Combinations of a Phone Number(手机几个按键对应的字母(符号)组合)相关推荐
- LeetCode Letter Combinations of a Phone Number
LeetCode解题之Letter Combinations of a Phone Number 原题 手机按键上每一个数字都相应了多个字母,如2相应了"abc",现给出一个数字串 ...
- Leetcode 17 - Letter Combinations of a Phone Number
题目 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意 已知在九宫格的输入法上,2对应的字符是a\b\c, ...
- 【DFS】LeetCode 17. Letter Combinations of a Phone Number
LeetCode 17. Letter Combinations of a Phone Number Solution1:我的答案 利用8皇后同样的方法,回溯+递归 时间复杂度O(3n)O(3n)O( ...
- [ LeetCode ] #17. Letter Combinations of a Phone Number(电话按键组合字符 C++ Python)
题目:17. Letter Combinations of a Phone Number Difficulty: Medium Given a string containing digits fro ...
- LeetCode | 0017. Letter Combinations of a Phone Number电话号码的字母组合【Python】
LeetCode 0017. Letter Combinations of a Phone Number电话号码的字母组合[Medium][Python][回溯][DFS][暴力] Problem L ...
- leetCode:Letter Combinations of a Phone Number
题目 Letter Combinations of a Phone Number Given a digit string, return all possible letter combinatio ...
- Leetcode #17 Letter Combinations of a Phone Number Z9键盘字母组合解题小节
1 题目理解 此道题目基于我们前几年用的,或许还有这几年用的九宫格输入法.九宫格上的每个数字对应着不同的字母,现在说,如果给定你一个数字,问你可以对应到多少种的字母组合? 这道题的解题方式,就是直接递 ...
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
最新文章
- R语言使用pROC包在同一图中绘制两条ROC曲线并通过假设检验检验ROC曲线的AUC或者偏AUC的差异(输出p值)
- No space left on device
- CHIL-ORACLE-唯一约束(unique)
- CSP认证201609-2	火车购票[C++题解]:模拟、vector、排序
- android倒计时功能,Android 实现列表倒计时功能
- php 通用购物车,PHP实现购物车代码[可重复使用]
- linux网络编程之网络字节序、主机字节序、大端、小端
- native层 安卓_安卓逆向——拼xx协议java层分析
- LeetCode 1785. 构成特定和需要添加的最少元素(贪心)
- Go 如何利用multipart/form-data实现文件的上传与下载
- Oracle存储过程介绍
- 手机通讯录备份代码实现二
- 231 · 自动补全
- smartsvn 忽略文件夹_MacOS下smartSVN使用教程
- 「NOIP2016」玩具谜题
- java访问https链接下载图片
- ROS集成开发环境 --- RoboWare(安装及学习笔记)
- r710服务器支持cpu列表,R720-2.5寸服务器支持哪些E5-的CPU型号
- Keepalived Nginx 类型效劳下高结实Linux系统计划
- js object转json