-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path18. Arithmetic Slices
45 lines (42 loc) · 1.1 KB
/
18. Arithmetic Slices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int numberOfArithmeticSlices(int[] A) {
int[] dp = new int[A.length];
int res = 0;
for(int i = 2; i < dp.length; i++) {
if(A[i]-A[i-1] == A[i-1]-A[i-2]) {
dp[i]=1+dp[i-1];
res+=dp[i];
}
}
return res;
}
}
public class Solution {
public int numberOfArithmeticSlices(int[] A) {
int dp = 0;
int sum = 0;
for (int i = 2; i < A.length; i++) {
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
dp = 1 + dp;
sum += dp;
} else
dp = 0;
}
return sum;
}
}
public class Solution {
public int numberOfArithmeticSlices(int[] A) {
int count = 0;
int sum = 0;
for (int i = 2; i < A.length; i++) {
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
count++;
} else {
sum += (count + 1) * (count) / 2;
count = 0;
}
}
return sum += count * (count + 1) / 2;
}
}