[Leetcode]_17 Letter Combinations of a Phone Number
来源:互联网 发布:先锋电视网络看电视 编辑:程序博客网 时间:2024/06/01 19:18
/** * Index: 17 * Title: Letter Combinations of a Phone Number * Author: ltree98 **/
这道题应该是用递归来做
digits.length() > 1 => 将 digits[0]对应的集合 与 digits.substr(1)返回的集合连接digits.length() == 1 => 返回数字对应的字符串集合
然后再处理一些无效值的情况:
- 数字中 0, 1, *, # 等其他值。 —— 这里用他们 ASCII码的差值来判断。
- 如果输入的数字中存在无效值,整个返回空。 —— 这里判断后面字符串的返回值是否为空,若为空,直接返回空。
class Solution {private: string map[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};public: vector<string> letterCombinations(string digits) { if(digits.length() == 0) return {}; int temp = digits[0] - '0'; if( temp < 2 || temp > 9 ) return {}; vector<string> ans = {}; string mapStr = map[temp]; if(digits.length() > 1) { vector<string> preAns = letterCombinations(digits.substr(1)); if(preAns.size() == 0) return {}; for(int i = 0; i < mapStr.length(); i++) { for(int j = 0; j < preAns.size(); j++) { ans.push_back(mapStr[i]+preAns[j]); } } } else { for(int i = 0; i < mapStr.length(); i++) { ans.push_back(mapStr.substr(i, 1)); } } return ans; }};
0 0
- [Leetcode]_17 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
- 计算π的值(难度系数:半颗星)
- linux设置安装的服务自动启动
- 第十五届“华为杯”大学生程序设计竞赛题解
- Caffe学习笔记(五):使用pycaffe生成solver.prototxt文件并进行训练
- web前端编程规范
- [Leetcode]_17 Letter Combinations of a Phone Number
- 性能测试流程阶段的工作
- 阿里云 ECS 部署:nginx+MySQL+Laravel+PHP7+Redis+Node.js
- 文本框
- 数据分页显示的简单实现
- js中return的用法
- acm_斐波那契数列
- C++类的大小及虚函数表
- sql 还原数据库 错误3154