# Challenge me, neat C++ solution, could be simpler?

• ``````int evalRPN(vector<string>& tokens) {
stack<int> stn;
for(auto s:tokens) {
if(s.size()>1 || isdigit(s[0])) stn.push(stoi(s));
else {
auto x2=stn.top(); stn.pop();
auto x1=stn.top(); stn.pop();
switch(s[0]) {
case '+': x1+=x2; break;
case '-': x1-=x2; break;
case '*': x1*=x2; break;
case '/': x1/=x2; break;
}
stn.push(x1);
}
}
return stn.top();
}``````

• Nice use of "auto" keyword!! :)

• your "stn.push(x1)" will be done even after "if",so it is wrong!! You can move it to each case.

• ``````// nice solution. since you are evaluating left to right, i will do right to left.
// you put 13 lines while i put 10 lines :)

class Solution {
public:

int calc(vector<string>& tokens, int start, int& end) {
string s=tokens[start];
end=start;
if (s=="+" || s=="-" || s=="*" || s=="/") {
int A=calc(tokens, start-1, end), B=calc(tokens, end-1, end);
if (s=="+") return A+B;
if (s=="-") return B-A;
if (s=="*") return A*B;
if (s=="/") return B/A;
}
return stoi(s);
}

int evalRPN(vector<string>& tokens) {
return calc(tokens, tokens.size()-1, *(new int));
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.