Posted on

Description

Submission

class Solution {
    int comb(int n) {
        return (1 + n - 2) * (n - 2) / 2;
    }
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int diff = 100000;
        int cur = -diff;
        int cnt = 1;
        int ret = 0;

        for(int i = 1; i < nums.size(); ++i) {
            cur = nums[i] - nums[i-1];
            if(cur != diff) {
                diff = cur;
                if(cnt >= 2) {
                    ret += comb(cnt + 1);
                }
                cnt = 1;
            } else {
                ++cnt;
            }
        }

        if(cnt >= 2) {
            ret += comb(cnt + 1);
        }

        return ret;
    }
};


// 1 2 3 4

Leave a Reply

Your email address will not be published. Required fields are marked *