Skip to content

Commit 1da56ea

Browse files
authoredJun 18, 2022
Adding solution for 682
1 parent a237872 commit 1da56ea

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎00682. Baseball Game.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
// Loop through ops, follow the instructions in a if-else blocks to create a final vec. Then loop through the final vec to calculate the result sum
3+
pub fn cal_points(ops: Vec<String>) -> i32 {
4+
let mut final_vec = Vec::new();
5+
let mut sum = 0;
6+
7+
for op in ops {
8+
let len = final_vec.len();
9+
10+
if op == "C" {
11+
final_vec.pop();
12+
} else if op == "D" {
13+
final_vec.push(final_vec[len - 1] * 2);
14+
} else if op == "+" {
15+
final_vec.push(final_vec[len - 1] + final_vec[len - 2]);
16+
} else {
17+
final_vec.push(op.parse::<i32>().unwrap());
18+
}
19+
}
20+
21+
for num in final_vec {
22+
sum += num;
23+
}
24+
25+
return sum;
26+
}
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.