-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path8Zf90G.rs
41 lines (37 loc) · 1.14 KB
/
8Zf90G.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
34
35
36
37
38
39
40
41
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
/// 逆波兰表达式配合栈
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
let mut stack = vec![];
for i in tokens {
match &*i {
"+" => {
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
stack.push(x + y);
}
"-" => {
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
stack.push(y - x);
}
"*" => {
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
stack.push(x * y);
}
"/" => {
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
stack.push(y / x);
}
s => {
stack.push(s.parse().unwrap());
}
}
}
stack.pop().unwrap()
}
}