-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150.cpp
38 lines (35 loc) · 1.1 KB
/
150.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
class Solution {
public:
int evalRPN(vector<string>& tokens) {
int size = tokens.size();
stack <long long> numbers;
//ascii for operators are below 48
long long a, b, temp;
for (int i = 0; i < size ; i++){
//basically just number, so just push to stack
temp = static_cast<int>(tokens[i][0]);
if (tokens[i].size() > 1 || temp >= 48){
numbers.push(stoi(tokens[i]));
}
else{
a = numbers.top();
numbers.pop();
b = numbers.top();
numbers.pop();
if(tokens[i] == "+"){
numbers.push(b+a);
}
else if(tokens[i] == "-"){
numbers.push(b-a);
}
else if(tokens[i] == "/"){
numbers.push(b/a);
}
else if(tokens[i] == "*"){
numbers.push(b*a);
}
}
}
return numbers.top();
}
};