MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / number_of_arithmetic_slices

Function number_of_arithmetic_slices

413-arithmetic-slices.rs:1–17  ·  view source on GitHub ↗
(nums: Vec<i32>)

Source from the content-addressed store, hash-verified

1pub 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
19fn main() {
20 println!("{:?}", number_of_arithmetic_slices(vec![1, 2, 3, 4]));

Callers

nothing calls this directly

Calls 1

is_arithmeticFunction · 0.85

Tested by

no test coverage detected