#413

Arithmetic Slices

specialist · 635 · lc medium +30 · verified · 64.8% accepted · 5,626 likes · top 68%

play →

Description

An arithmetic subarray has at least three elements and a constant difference between every pair of consecutive elements (for example [1,3,5,7,9], [7,7,7,7], [3,-1,-5,-9]).

Given an integer array nums, count and return the total number of arithmetic contiguous subarrays.

Example 1:

Input: nums = [1,2,3,4]
Output: 3
Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.

Example 2:

Input: nums = [1]
Output: 0

Code

1
2
3