mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #1729 from Jack-Zhang-1314/patch-3
update 0150.逆波兰表达式求值.md about rust
This commit is contained in:
@ -420,6 +420,41 @@ object Solution {
|
|||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
|
||||||
|
let mut stack = vec![];
|
||||||
|
for token in tokens.into_iter() {
|
||||||
|
match token.as_str() {
|
||||||
|
"+" => {
|
||||||
|
let a = stack.pop().unwrap();
|
||||||
|
*stack.last_mut().unwrap() += a;
|
||||||
|
}
|
||||||
|
"-" => {
|
||||||
|
let a = stack.pop().unwrap();
|
||||||
|
*stack.last_mut().unwrap() -= a;
|
||||||
|
}
|
||||||
|
"*" => {
|
||||||
|
let a = stack.pop().unwrap();
|
||||||
|
*stack.last_mut().unwrap() *= a;
|
||||||
|
}
|
||||||
|
"/" => {
|
||||||
|
let a = stack.pop().unwrap();
|
||||||
|
*stack.last_mut().unwrap() /= a;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
stack.push(token.parse::<i32>().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stack.pop().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user