rustgym/leetcode/
_413_arithmetic_slices.rs1struct Solution;
2
3impl Solution {
4 fn number_of_arithmetic_slices(a: Vec<i32>) -> i32 {
5 let mut slice = vec![];
6 let mut res = 0;
7 for x in a {
8 if slice.len() < 2 {
9 slice.push(x);
10 } else {
11 let y = slice.pop().unwrap();
12 let z = slice.pop().unwrap();
13 if y - z == x - y {
14 slice.push(z);
15 slice.push(y);
16 slice.push(x);
17 res += slice.len() - 2;
18 } else {
19 slice.clear();
20 slice.push(y);
21 slice.push(x);
22 }
23 }
24 }
25 res as i32
26 }
27}
28
29#[test]
30fn test() {
31 let a = vec![1, 2, 3, 4];
32 let res = 3;
33 assert_eq!(Solution::number_of_arithmetic_slices(a), res);
34}