forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
48 lines (39 loc) · 1.04 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// Source : https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
/// Author : liuyubobobo
/// Time : 2018-08-29
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
/// Two stacks
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> nums;
stack<char> ops;
for(const string& s: tokens){
if(s == "+" || s == "-" || s == "*" || s == "/"){
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
if(s == "+")
nums.push(b + a);
else if(s == "-")
nums.push(b - a);
else if(s == "*")
nums.push(b * a);
else if(s == "/")
nums.push(b / a);
}
else
nums.push(atoi(s.c_str()));
}
return nums.top();
}
};
int main() {
return 0;
}