版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shey666/article/details/80786923
Leetcode 17
Letter Combinations of a Phone Number
- Problem Description:
电话上每个数字都有对应的字母,如下图所示,数字0和数字1没有对应的字母,数字2对应的字母包括“abc”,现输入一串数字要求输出所有可能的字母组合。
具体的题目信息:
https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ - Example:
- Solution: 【参考讨论区代码】
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if (digits.length() == 0) return res;
res.push_back("");
vector<string> temp;
vector<string> Phone={"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for(int i = 0; i < digits.length(); i++) {
int num = digits[i]-'0';
if(num<0||num>9) break;
string t = Phone[num];
if (t.length() == 0) continue;
for (int k = 0;k < t.length(); k++) {
for (int j = 0; j < res.size(); j++) {
temp.push_back(res[j]+t[k]);
}
}
res.swap(temp);
temp.clear();
}
return res;
}
};