Skip to content

Commit a1c2975

Browse files
committed
Create 413-arithmetic-slices.rs
1 parent 5d99bb7 commit a1c2975

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

413-arithmetic-slices.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub fn number_of_arithmetic_slices(nums: Vec<i32>) -> i32 {
2+
if nums.len() < 3 { return 0; }
3+
pub fn is_arithmetic(nums: &[i32]) -> bool {
4+
let minus = nums[1] - nums[0];
5+
for i in 2..nums.len() {
6+
if nums[i] - nums[i-1] != minus { return false; }
7+
}
8+
true
9+
}
10+
let mut ret = 0;
11+
for length in 3..=nums.len() {
12+
for i in 0..=nums.len() - length {
13+
if is_arithmetic(&nums[i..i+length]) { ret += 1; }
14+
}
15+
}
16+
ret
17+
}
18+
19+
fn main() {
20+
println!("{:?}", number_of_arithmetic_slices(vec![1, 2, 3, 4]));
21+
}

0 commit comments

Comments
 (0)