Skip to content

Commit b5decfe

Browse files
authored
Add function to count arithmetic slices
1 parent 76bc3aa commit b5decfe

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var numberOfArithmeticSlices = function(nums) {
6+
let count = 0;
7+
let ans = 0;
8+
9+
if (nums.length <= 2) return 0;
10+
11+
for (let i = 2; i<nums.length; i++){
12+
if ((nums[i] - nums[i-1]) == (nums[i-1] - nums[i-2])){
13+
count++;
14+
ans += count;
15+
} else {
16+
count = 0;
17+
}
18+
}
19+
20+
return ans;
21+
};

0 commit comments

Comments
 (0)