Skip to content

Commit 81e6982

Browse files
committed
feat: resolved
1 parent 4699b56 commit 81e6982

File tree

1 file changed

+23
-1
lines changed
  • evaluate_reverse_polish_notation/src

1 file changed

+23
-1
lines changed

evaluate_reverse_polish_notation/src/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1+
const OPERATORS: [&str; 4] = ["+", "-", "*", "/"];
2+
13
pub struct Solution {}
24

35
impl Solution {
4-
pub fn eval_rpn(tokens: Vec<String>) -> i32 {}
6+
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
7+
let mut nums: Vec<i32> = vec![];
8+
for t in tokens.iter() {
9+
if !OPERATORS.iter().any(|x| x == t) {
10+
nums.push(t.parse().unwrap());
11+
continue;
12+
}
13+
let n2 = nums.pop().unwrap();
14+
let n1 = nums.pop().unwrap();
15+
nums.push(if t == "+" {
16+
n1 + n2
17+
} else if t == "-" {
18+
n1 - n2
19+
} else if t == "*" {
20+
n1 * n2
21+
} else {
22+
n1 / n2
23+
})
24+
}
25+
nums.pop().unwrap()
26+
}
527
}
628

729
#[cfg(test)]

0 commit comments

Comments
 (0)