类型: 字符串匹配+替换
开始的思路: 用replace多次扫过,卡在最后5个测试用例
最终思路:利用堆栈、队列的思路:
- 设置标志位即可
class Solution {
public:
map<string,string> mp;
string evaluate(string s, vector<vector<string>>& knowledge) {
for(auto t:knowledge)
mp[t[0]]=t[1];
string temp="",stem="";
int flag=0;
for(auto &t:s){
if(t=='(') {
flag=1;
continue;
}
if(t==')'){
if(mp[stem]!="") temp+=mp[stem];
else temp+='?';
stem="";
flag=0;
continue;
}
if(!flag) temp+=t;
else stem+=t;
}
return temp;
}
};