Very simple and short O(n) time and O(1) space


  • 0
    var numberOfArithmeticSlices = function(A) {
        let res = 0, i = 0;
        while (i < A.length - 2) {
            const start = i;
            while (A[i + 2] + A[i] === 2 * A[i + 1])
                res += (i++) + 1 - start;
            i++;
        }
        return res;
    };
    

Log in to reply
 

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