leetcode_rust/
baseball_game.rs1#![allow(dead_code)]
2
3pub fn cal_points(ops: Vec<String>) -> i32 {
4 let mut nums = vec![];
5 for s in ops {
6 match s.as_str() {
7 "+" => {
8 nums.push(nums[nums.len() - 1] + nums[nums.len() - 2]);
9 }
10 "D" => {
11 nums.push(nums[nums.len() - 1] * 2);
12 }
13 "C" => {
14 nums.pop();
15 }
16 num => {
17 nums.push(num.parse::<i32>().unwrap());
18 }
19 };
20 }
21
22 nums.iter().sum()
23}
24
25#[cfg(test)]
26mod tests {
27 use super::*;
28
29 #[test]
30 fn test1() {
31 let ops = vec![
32 String::from("5"),
33 String::from("-2"),
34 String::from("4"),
35 String::from("C"),
36 String::from("D"),
37 String::from("9"),
38 String::from("+"),
39 String::from("+"),
40 ];
41 assert_eq!(cal_points(ops), 27);
42 }
43}