Descritpion Link to heading

413.arithmetic slices

Solution Link to heading

We should notice that the number of subarray $cnt = len - 3 + 1 + len - 4 + 1 + … + 1$(len) is the length of arithmetic array.

Code Link to heading

class Solution {
  public:
    int numberOfArithmeticSlices(vector<int> &nums) {
        if (nums.size() < 3)
            return 0;
        int dif = nums[1] - nums[0];
        int len = 2;
        int cnt = 0;
        for (int i = 2; i < nums.size(); i++) {
            if (nums[i] - nums[i - 1] == dif)
                len++;
            else {
                if (len >= 3)
                    cnt += (len - 2) * (len - 1) / 2;
                len = 2;
                dif = nums[i] - nums[i - 1];
            }
        }
        if (len >= 3)
            cnt += (len - 2) * (len - 1) / 2;
        return cnt;
    }
};