代码拉取完成,页面将自动刷新
// Time: O(n^2)
// Space: O(n^2)
class Solution {
public:
int evaluate(string expression) {
vector<string> tokens{""};
unordered_map<string, string> lookup;
vector<pair<vector<string>, unordered_map<string, string>>> stk;
for (const auto& c : expression) {
if (c == '(') {
if (tokens[0] == "let") {
evaluate(tokens, &lookup);
}
stk.emplace_back(move(tokens), lookup);
tokens = {""};
} else if (c == ' ') {
tokens.emplace_back();
} else if (c == ')') {
const auto& val = evaluate(tokens, &lookup);
tie(tokens, lookup) = move(stk.back());
stk.pop_back();
tokens.back() += val;
} else {
tokens.back().push_back(c);
}
}
return stoi(tokens[0]);
}
private:
string evaluate(const vector<string>& tokens, unordered_map<string, string>* lookup) {
static const unordered_set<string> operators{"add", "mult"};
if (operators.count(tokens[0])) {
const auto& a = stoi(getval(*lookup, tokens[1]));
const auto& b = stoi(getval(*lookup, tokens[2]));
return to_string(tokens[0] == "add" ? a + b : a * b);
}
for (int i = 1; i < tokens.size() - 1; i += 2) {
if (!tokens[i + 1].empty()) {
(*lookup)[tokens[i]] = getval(*lookup, tokens[i + 1]);
}
}
return getval(*lookup, tokens.back());
}
string getval(const unordered_map<string, string>& lookup, const string& x) {
return lookup.count(x) ? lookup.at(x) : x;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。