Simple C++ O(n) Time O(1) Space Solution.


  • 0
    F
    class Solution {
    public:
        int numberOfArithmeticSlices(vector<int>& nums) {
            int ans = 0, count = 2;
            for (int i = 2; i < nums.size(); ++ i)
                if (nums[i - 1] - nums[i - 2] != nums[i] - nums[i - 1])
                {
                    ans += (count - 2) * (count - 1) / 2;
                    count = 2;
                }else
                    count ++;
            ans += (count - 2) * (count - 1) / 2;
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.