您的位置:首页 > 其它

leetcode--17. Letter Combinations of a Phone Number

2017-09-29 14:51 579 查看
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.



利用backtrace。当探到底时,可以push_back

class Solution {
public:

unordered_map<char, string> hash;
vector<string> ret;

void bt(string digits, int index, string former_str) {
char key = digits[index];
string val = hash[key];
for(int i = 0; i < val.length(); i++){
string tmp_str = former_str + val[i];
if(index == digits.length()-1) {
ret.push_back(tmp_str);
}
else{
bt(digits, index + 1, tmp_str);
}
}
}

vector<string> letterCombinations(string digits) {
hash.insert({{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl
4000
"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}});
bt(digits, 0, "");
return ret;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: