leetcode 17:Letter Combinations of a Phone Number(15-10-5)
2015-10-05 19:35
495 查看
1.Problem
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.
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
2.Solution
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits[0]=='\0') {return res;}
string charmap[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.push_back("");
for (int i = 0; i < digits.size(); i++)
{
vector<string> tempres;
string chars = charmap[digits[i] - '0'];
for (int c = 0; c < chars.size();c++)
for (int j = 0; j < res.size();j++)
tempres.push_back(res[j]+chars[c]);
res = tempres;
}
return res;
}
};
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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
2.Solution
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits[0]=='\0') {return res;}
string charmap[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.push_back("");
for (int i = 0; i < digits.size(); i++)
{
vector<string> tempres;
string chars = charmap[digits[i] - '0'];
for (int c = 0; c < chars.size();c++)
for (int j = 0; j < res.size();j++)
tempres.push_back(res[j]+chars[c]);
res = tempres;
}
return res;
}
};
相关文章推荐
- java记——随机数三个数的和
- 浅谈Linux下Makefile编写
- 大数据下的日志--ElasticSearch部分(四)--Mapping
- linux内存管理
- 【数据库】MFC ODBC(一)
- 51nod_learn_greedy_活动安排2
- 大数据下的日志--ElasticSearch部分(一)--初识
- CodeForcesGym 100753F Divisions
- ”Hello World!“的前世今生
- LightOJ 1282 Leading and Trailing
- RecyclerView 解析
- The Shawshank Redemption
- 大数据下的日志--ElasticSearch部分(三)--Bulk,Search操作
- Writing Fast Matlab code 1
- 怎么清洗眼镜片?
- CYC-AVPlayer播放器的简单使用
- static 使用之静态方法
- linux高级技巧:heartbeat+lvs(一)
- Objective-C NSData和NSMutableData
- iOS编程-------UITableViewEdit_move -----> UITableView 编辑