Leetcode 413 Solution
This article provides solution to leetcode question 413 (arithmetic-slices)
Access this page by simply typing in "lcs 413" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/arithmetic-slices
Solution
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int last_diff = -1;
int len = 0;
int total = 0;
for (int i = 2; i < A.size(); i++)
{
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2])
{
if (len == 0)
len = 3;
else
len++;
}
else
{
if (len > 2)
total += (len - 1) * (len - 2) / 2;
len = 0;
}
}
if (len > 2)
total += (len - 1) * (len - 2) / 2;
return total;
}
};