20240520 Finished

This commit is contained in:
jackfiled 2024-05-20 11:19:27 +08:00
parent 53cf95499d
commit 24ff050e3a
2 changed files with 45 additions and 1 deletions

View File

@ -135,4 +135,5 @@ mod p56_merge_intervals;
mod p57_insert_interval;
mod p452_minimum_number_of_arrows_to_burst_balloons;
mod p71_simplify_path;
mod p155_min_stack;
mod p155_min_stack;
mod p150_evaluate_reverse_polish_notation;

View File

@ -0,0 +1,43 @@
/**
* [150] Evaluate Reverse Polish Notation
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
let mut stack = vec![];
for token in tokens {
if let Ok(num) = token.parse::<i32>() {
stack.push(num);
} else {
let second = stack.pop().unwrap();
let first = stack.pop().unwrap();
match token.as_str() {
"+" => stack.push(first + second),
"-" => stack.push(first - second),
"*" => stack.push(first * second),
"/" => stack.push(first / second),
_ => {},
}
}
}
stack[0]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_150() {
assert_eq!(9, Solution::eval_rpn(vec_string!["2","1","+","3","*"]));
}
}