Iterative solution using stack


#1

int Solution::evalRPN(vector &A) {
stacks;
for(int i=0;i<A.size();i++)
{
if(A[i]=="+" || A[i]=="-"||A[i]==""||A[i]=="/")
{
int x=stoi(s.top());
s.pop();
int y=stoi(s.top());
s.pop();
if(A[i]=="+")
{
s.push(to_string(x+y));
}
else if(A[i]=="-")
{
s.push(to_string(y-x));
}
else if(A[i]=="
")
{
s.push(to_string(x*y));
}
else
{
s.push(to_string(y/x));
}
}
else
{
s.push(A[i]);
}
}
if(s.size()==1)
return stoi(s.top());
else
return 0;

}