mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
update 0150.逆波兰表达式求值.md about rust
This commit is contained in:
@ -89,12 +89,8 @@ C++代码如下:
|
||||
class Solution {
|
||||
public:
|
||||
int evalRPN(vector<string>& tokens) {
|
||||
<<<<<<< HEAD
|
||||
stack<long long> st;
|
||||
=======
|
||||
// 力扣修改了后台测试数据,需要用longlong
|
||||
stack<long long> st;
|
||||
>>>>>>> 28f3b52a82e3cc650290fb02030a53900e122f43
|
||||
for (int i = 0; i < tokens.size(); i++) {
|
||||
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
|
||||
long long num1 = st.top();
|
||||
@ -424,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">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user