代码拉取完成,页面将自动刷新
## 17. Letter Combinations of a Phone Number
Given a string containing digits from 2-9
inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example 1:
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:
Input: digits = ""
Output: []
Example 3:
Input: digits = "2"
Output: ["a","b","c"]
Constraints:
0 <= digits.length <= 4
digits[i]
is a digit in the range ['2', '9']
.class Solution{
public:
const vector<string> pad = {
"", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"
};
vector<string> letterCombinations(string digits){
if (digits.empty()) return {};
vector<string> result;
result.push_back("");
for(auto digit: digits){
vector<string> tmp;
for (auto candidate:pad[digit - '0']){
for(auto s:result){
tmp.push_back(s+candidate);
}
}
result.swap(tmp);
}
return result;
}
};
More clear:
class Solution {
public:
vector<string> letterCombinations(string digits){
if (digits.empty()) return {};
vector<string> res;
res.push_back("");
map<char,string> map;
map = {{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}};
for (int i=0; i<digits.size(); ++i){
if(digits[i]<'2' || digits[i]>'9'){
continue;
}
string cand = map[digits[i]];
vector<string> tmp;
for (int j=0;j < cand.size(); ++j){
for (int k=0; k<res.size(); ++k){
tmp.push_back(res[k] + cand[j]);
}
}
res = tmp;
}
return res;
}
};
def letterCombinations(self, digits):
dict = {'2':"abc", '3':"def", '4':"ghi", '5':"jkl", '6':"mno", '7': "pqrs",
'8':"tuv", '9':"wxyz"}
cmb = [''] if digits else []
for d in digits:
cmb = [p+q for p in cmb for q in dict[d]]
return cmb
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。