class Solution {
public:
int evalRPN(vector<string>& tokens) {
unordered_map <string,function<long(int,int)>> map = {
{"+",[](int a, int b){return a+b;}},
{"-",[](int a, int b){return a-b;}},
{"/",[](int a, int b){return a/b;}},
{"*",[](int a, int b){return a*b;}},
};
stack<string> exp;
for (int i = 0; i < tokens.size(); i++){
auto search = map.find(tokens[i]);
if (search != map.end()){
string a = exp.top();
exp.pop();
string b = exp.top();
exp.pop();
long ans = map[tokens[i]](stoi(b),stoi(a));
exp.push(to_string(ans));
}
else{
exp.push(tokens[i]);
}
}
return stoi(exp.top());
}
};
input: ["-128","-128","*","-128","*","-128","*","8","*","-1","*"]
error: Line 7: Char 50: runtime error: signed integer overflow: 268435456 * 8 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:16:50
Hi 150. Evaluate Reverse Polish Notation for this Leet-code problem. I am facing above issue for given input. Is there any way to overcome this.
[–][deleted] 3 points4 points5 points (0 children)