-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlib.rs
59 lines (55 loc) · 1.34 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
struct Solution;
impl Solution {
pub fn cal_points(ops: Vec<String>) -> i32 {
let mut stack: Vec<i32> = Vec::new();
for op in ops {
match op.as_str() {
"+" => {
let sum = stack[stack.len() - 1] + stack[stack.len() - 2];
stack.push(sum);
}
"D" => {
let len = stack.len();
stack.push(stack[len - 1] * 2);
}
"C" => {
stack.pop();
}
_ => {
stack.push(op.parse().unwrap());
}
}
}
let mut ret = 0;
for num in stack {
ret += num;
}
ret
}
}
#[test]
fn it_works() {
assert_eq!(
Solution::cal_points(vec![
String::from("5"),
String::from("2"),
String::from("C"),
String::from("D"),
String::from("+")
]),
30
);
assert_eq!(
Solution::cal_points(vec![
String::from("5"),
String::from("-2"),
String::from("4"),
String::from("C"),
String::from("D"),
String::from("9"),
String::from("+"),
String::from("+")
]),
27
);
}