3ms C++ Sliding Window


  • 0
    L
    int numberOfArithmeticSlices(vector<int>& A) {
            if(A.size() < 3) return 0;
            int first = 0, end = 1, diff = A[1] - A[0];
            int counter = 0;
            while(end < A.size()) {
                if(end - first < 2)
                    end++;
                if(end == A.size()) break;
                int tmp = A[end] - A[end-1];
                if(tmp != diff) {
                    counter += (1+(end-first+1-3)) * (end-first+1-3)/2;
                    first = end-1;
                    diff = tmp;
                } else
                    end++;
            }
            counter += (1+(end-first+1-3)) * (end-first+1-3)/2;
            return counter;
        }
    

Log in to reply
 

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