forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0150-evaluate-reverse-polish-notation.rs
33 lines (31 loc) · 1.19 KB
/
0150-evaluate-reverse-polish-notation.rs
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
impl Solution {
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
let mut stack: Vec<i32> = Vec::new();
for token in tokens {
match &token[..] {
"+" => {
let second_operand = stack.pop().unwrap();
let first_operand = stack.pop().unwrap();
stack.push(first_operand + second_operand)
}
"-" => {
let second_operand = stack.pop().unwrap();
let first_operand = stack.pop().unwrap();
stack.push(first_operand - second_operand)
}
"*" => {
let second_operand = stack.pop().unwrap();
let first_operand = stack.pop().unwrap();
stack.push(first_operand * second_operand)
}
"/" => {
let second_operand = stack.pop().unwrap();
let first_operand = stack.pop().unwrap();
stack.push(first_operand / second_operand)
}
value => stack.push(value.parse::<i32>().unwrap()),
}
}
stack[0]
}
}