以下是我提交的
https://leetcode.com/problems/letter-combinations-of-a-phone-number
然而,我看到线上非常奇怪的行为
char c = map1[s[i]][j];
t.replace(i,1,1,c);
矢量map1 [s [i]]正在改变下面代码中上行执行的大小。如何通过值而不是引用来复制字符c。如果我在函数参数中删除了字符串的引用(意思是没有&),一切正常,但它不是正确的方法,因为按值复制字符串是昂贵的。
class Solution {
public:
string s;
vector<string> res;
unordered_map<char, vector<char>> map1;
void letterCombinations(string &t, int i){
char c;
if(s.size()==0)
return;
if(i==s.size())
res.push_back(t);
else{
for(int j=0;j<map1[s[i]].size();j++){
//cout<<"Starting:"<<map1[s[i]].size()<<endl;
char c = map1[s[i]][j];
t.replace(i,1,1,c);
letterCombinations(t, i+1);
//cout<<"Return:"<<map1[s[i]].size()<<endl;
}
}
}
vector<string> letterCombinations(string digits) {
stringstream t;
map1['1'] = vector<char>{};
map1['0'] = vector<char>{};
map1['2'] = vector<char>{'a','b','c'};
map1['3'] = vector<char>{'d','e','f'};
map1['4'] = vector<char>{'g','h','i'};
map1['5'] = vector<char>{'j','k','l'};
map1['6'] = vector<char>{'m','n','o'};
map1['7'] = vector<char>{'p','q','r','s'};
map1['8'] = vector<char>{'t','u','v'};
map1['9'] = vector<char>{'w','x','y','z'};
s=digits;
letterCombinations(s,0);
return res;
}
};