-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate-reverse-polish-notation.rs
More file actions
36 lines (35 loc) · 1.13 KB
/
evaluate-reverse-polish-notation.rs
File metadata and controls
36 lines (35 loc) · 1.13 KB
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
// https://leetcode.com/problems/evaluate-reverse-polish-notation
impl Solution {
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
let mut stack: Vec<String> = Vec::new();
for t in tokens {
if t == "+" || t == "-" || t == "*" || t == "/" {
let b: i32 = stack
.pop()
.unwrap()
.parse::<i32>()
.unwrap();
let a: i32 = stack
.pop()
.unwrap()
.parse::<i32>()
.unwrap();
stack.push(
{
if t == "+" { a + b }
else if t == "-" { a - b }
else if t == "*" { a * b }
else { a / b }
}.to_string()
);
} else {
stack.push(t);
}
}
stack
.pop()
.unwrap()
.parse::<i32>()
.unwrap()
}
}