-
Notifications
You must be signed in to change notification settings - Fork 2
/
arithmetic-slices.rs
48 lines (41 loc) · 1.07 KB
/
arithmetic-slices.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
#![allow(dead_code, unused, unused_variables)]
fn main() {
println!(
"{}",
Solution::number_of_arithmetic_slices(vec![1, 2, 3, 4])
);
println!(
"{}",
Solution::number_of_arithmetic_slices(vec![1, 2, 3, 4, 5, 6])
);
println!(
"{}",
Solution::number_of_arithmetic_slices(vec![1, 2, 3, 4, 10, 11, 12, 13])
);
}
struct Solution;
impl Solution {
pub fn number_of_arithmetic_slices(nums: Vec<i32>) -> i32 {
if nums.len() < 3 {
return 0;
}
let mut count = 0;
let mut num = 2;
let mut s = nums[1] - nums[0];
for i in 2..nums.len() {
if nums[i] - nums[i - 1] == s {
num += 1;
} else {
if num >= 3 {
count += (1..=num - 3 + 1).into_iter().sum::<i32>();
}
s = nums[i] - nums[i - 1];
num = 2;
}
}
if num >= 3 {
count += (1..=num - 3 + 1).into_iter().sum::<i32>();
}
count
}
}