# leetcode 017 —— Letter Combinations of a Phone Number

365人阅读 评论(0)

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"].

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> table={"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> res;
if (digits.empty()) return res;
scan(0, "", digits, table, res);
return res;

}
void scan(int level, string path, string &digits, vector<string> &table,vector<string> &res){
if (level == digits.size()){
res.push_back(path);
return;
}
for (int i = 0; i < table[digits[level] - '0'].size(); i++){
path.push_back(table[digits[level] - '0'][i]);
scan(level + 1, path, digits, table, res);
path.pop_back();
}
}
};

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：26135次
• 积分：2054
• 等级：
• 排名：第18682名
• 原创：188篇
• 转载：0篇
• 译文：0篇
• 评论：1条
阅读排行
评论排行
最新评论