Leetcode--17. Letter Combinations of a Phone Number
来源:互联网 发布:vip视频解析原理知乎 编辑:程序博客网 时间:2024/06/06 03:08
题目: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”].
思路如下:
- 把数字表示的字符映射到数组中,初始化result为{“”}。
- 对每一个digit[i]中的数字字符:
- 取result的队首元素,分别与digit[i]中的每个字符连接,放入队列,并删除该队首元素。
注意要先判断digits是否为空,然后再初始化result,测试用例第一个就是digits为空。
C++代码如下:
vector<string> letterCombinations(string digits) { vector<string> result; if (digits.empty()) return result; result.push_back(""); vector<string> letter = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" }; for (int i = 0; i < digits.size(); i++) { int sz = result.size(); //一定先记录下当前result的size,否则会陷入死循环,因为下面的操作会改变result的size. for (int j = 0; j < sz; j++) { string temp = result[0]; result.erase(result.begin()); for (int k = 0; k < letter[digits[i] - '0'].size(); k++) { result.push_back(temp + letter[digits[i] - '0'][k]); } } } return result;}
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
- mac os下使用 Docker安装oracle-xe-11g数据库
- 算法学习-(二)动态规划算法
- mac docker设置阿里云镜像
- Odoo8.0在CentOS7的安装步骤
- Eclipse neon 安装SVN插件
- Leetcode--17. Letter Combinations of a Phone Number
- 项目中使用的以一周为时间段查询自动填充时间
- Python爬虫防封杀方法集合
- 基于图像识别的分类器的实现
- Problem E: STL——呵呵型自动机
- 并行计算之OpenMPx
- Java IO
- 2017年PHP程序员未来路在何方
- Odoo8.0在CentOS7的安装步骤