今天的题目很简单,主要就是迭代。
注意:我一开始用的是vector<char>,然后好像单个的char是不能变成string,还是python方便啊。好像需要先转换成一个char[] = {ch, 0},才能变成string,原因如下。
class Solution {
public:
map<char, vector<string>> digi = {{'2',vector<string>{"a","b","c"}},
{'3',vector<string>{"d","e","f"}},
{'4',vector<string>{"g","h","i"}},
{'5',vector<string>{"j","k","l"}},
{'6',vector<string>{"m","n","o"}},
{'7',vector<string>{"p","q","r","s"}},
{'8',vector<string>{"t","u","v"}},
{'9',vector<string>{"w","x","y","z"}}};
vector<string> letterCombinations(string digits) {
vector<string> ret;
if(digits.size()==0) return ret;
vector<string> ret_temp = letterCombinations(digits.substr(1, digits.size()-1));
if(ret_temp.empty()){
for(int j=0; j<digi[digits[0]].size(); j++){
ret.push_back(digi[digits[0]][j]);
}
return ret;
}
for(int i=0;i<ret_temp.size(); i++){
for(int j=0; j<digi[digits[0]].size(); j++){
ret.push_back(digi[digits[0]][j]+ret_temp[i]);
}
}
return ret;
}
};
早睡。模式识别蛮难的,好多原理的知识。按照贝叶斯学派,参数也是概率分布。