@zhan430 Why not use a variable instead of an array?

class Solution(object):
def numberOfArithmeticSlices(self, A):
curr, sum = 0, 0
for i in range(2,len(A)):
if A[i]-A[i-1] == A[i-1]-A[i-2]:
curr += 1
sum += curr
else:
curr = 0
return sum

In the past, I have solved that problem like that. But I like your way.

public class Solution {
public int numberOfArithmeticSlices(int[] A) {
if(A.length<=2) return 0;
int ans= 0;
long diff = Long.MIN_VALUE;
int prev = A[0];
int len = 1;
for(int i = 1;i<A.length;i++){
if(diff == Long.MIN_VALUE || diff == A[i] - prev){
diff = A[i] - prev;
len++;
prev = A[i];
} else {
ans+=countSlices(len);
len = 1;
diff = Long.MIN_VALUE;
i--;
}
}
ans+=countSlices(len);
return ans;
}
public int countSlices(int len){
int ans = 0;
for(int i = 3;i<=len;i++){
ans+=len-i+1;
}
return ans;
}
}

public int numberOfArithmeticSlices(int[] A) {
int res = 0;
if (A == null || A.length < 3)
return res;
int start = 0;
while (start + 2 < A.length) {
int offset = A[start + 1] - A[start];
int i = start + 2;
while (i < A.length && A[i] - A[i - 1] == offset)
i++;
int n = i - start;
if (n >= 3) res += (n - 1) * (n - 2) / 2;
start = i - 1;
}
return res;
}