20240520 Finished
This commit is contained in:
parent
53cf95499d
commit
24ff050e3a
|
@ -136,3 +136,4 @@ mod p57_insert_interval;
|
||||||
mod p452_minimum_number_of_arrows_to_burst_balloons;
|
mod p452_minimum_number_of_arrows_to_burst_balloons;
|
||||||
mod p71_simplify_path;
|
mod p71_simplify_path;
|
||||||
mod p155_min_stack;
|
mod p155_min_stack;
|
||||||
|
mod p150_evaluate_reverse_polish_notation;
|
43
src/problem/p150_evaluate_reverse_polish_notation.rs
Normal file
43
src/problem/p150_evaluate_reverse_polish_notation.rs
Normal 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","*"]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user