Skip to content

Commit 44c876e

Browse files
authored
Merge pull request #1647 from AkifhanIlgaz/0150
0150
2 parents 75fea9c + 7afd359 commit 44c876e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
3+
let mut stack: Vec<i32> = Vec::new();
4+
5+
for token in tokens {
6+
match &token[..] {
7+
"+" => {
8+
let second_operand = stack.pop().unwrap();
9+
let first_operand = stack.pop().unwrap();
10+
stack.push(first_operand + second_operand)
11+
}
12+
"-" => {
13+
let second_operand = stack.pop().unwrap();
14+
let first_operand = stack.pop().unwrap();
15+
stack.push(first_operand - second_operand)
16+
}
17+
"*" => {
18+
let second_operand = stack.pop().unwrap();
19+
let first_operand = stack.pop().unwrap();
20+
stack.push(first_operand * second_operand)
21+
}
22+
"/" => {
23+
let second_operand = stack.pop().unwrap();
24+
let first_operand = stack.pop().unwrap();
25+
stack.push(first_operand / second_operand)
26+
}
27+
value => stack.push(value.parse::<i32>().unwrap()),
28+
}
29+
}
30+
31+
stack[0]
32+
}
33+
}

0 commit comments

Comments
 (0)