Skip to content

Commit

Permalink
Time: 17 ms (70.72%), Space: 11.9 MB (93.63%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Dec 17, 2022
1 parent d44b7dc commit 5667210
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
int solve(string op,int x,int y)
{
if(op=="+")return x+y;
if(op=="-")return x-y;
if(op=="*")return long(x)*long(y);
if(op=="/")return x/y;
return -1;
}
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]=="+"||tokens[i]=="-"||tokens[i]=="*"||tokens[i]=="/")
{
int y=st.top();
st.pop();
int x=st.top();
st.pop();
st.push(solve(tokens[i],x,y));
}
else
{
st.push(stoi(tokens[i]));
}
}
return st.top();

}
};

0 comments on commit 5667210

Please sign in to comment.