150. 逆波兰表达式求值

 1 class Solution 
 2 {
 3 public:
 4     int evalRPN(vector<string>& tokens) 
 5     {
 6         stack<int> s;
 7         int n = tokens.size();
 8         for(int i = 0;i < n;i ++)
 9         {
10             if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
11             {
12                 int b = s.top();s.pop();
13                 int a = s.top();s.pop();
14                 if(tokens[i] == "+") s.push(a + b);
15                 if(tokens[i] == "-") s.push(a - b);
16                 if(tokens[i] == "*") s.push(a * b);
17                 if(tokens[i] == "/") s.push(a / b);
18             }
19             else s.push(stoi(tokens[i]));
20         }
21         return s.top();
22     }
23 };

 

上一篇:dis ip routing-table


下一篇:「拼多多」Java岗150道面试题:并发编程+多线程+spring+Redis等