[leetcode]17. Letter Combinations of a Phone Number
Analysis
fighting—— [每天刷题并不难0.0]
Given a string containing digits from 2-9 inclusive, 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 that 1 does not map to any letters.
Explanation:
Pretty simple one.
Implement
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
string letters[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int len = digits.size();
if(len<=0)
return res;
res.push_back("");
for(int i=0; i<len; i++){
vector<string> tmp;
string letter = letters[digits[i]-'0'];
for(int j=0; j<letter.size(); j++){
for(int k=0; k<res.size(); k++)
tmp.push_back(res[k]+letter[j]);
}
res.swap(tmp);
}
return res;
}
};