-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path150.cpp
More file actions
executable file
·27 lines (27 loc) · 1002 Bytes
/
150.cpp
File metadata and controls
executable file
·27 lines (27 loc) · 1002 Bytes
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
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> value;
int index = 0;
while (index < tokens.size() && tokens[index] != "+" && tokens[index] != "-" && tokens[index] != "*" && tokens[index] != "/"){
value.push(stoi(tokens[index]));
index++;
}
while (index < tokens.size()){
if (tokens[index] != "+" && tokens[index] != "-" && tokens[index] != "*" && tokens[index] != "/"){
value.push(stoi(tokens[index]));
}
else{
if (a + b)
int a = value.top();value.pop();
int b = value.top();value.pop();
if (tokens[index] == "+") value.push(a+b);
if (tokens[index] == "-") value.push(b-a);
if (tokens[index] == "*") value.push(a*b);
if (tokens[index] == "/") {value.push(b/a);}
}
index++;
}
return value.top();
}
};