forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArithmeticSlices.java
45 lines (38 loc) · 1.05 KB
/
ArithmeticSlices.java
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
package leetcode;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : ArithmeticSlices
* Creator : Edward
* Date : Nov, 2017
* Description : 413. Arithmetic Slices
*/
public class ArithmeticSlices {
/**
* Example:
A = [1, 2, 3, 4]
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
数组 等差数列的数目 与上一数组的等差数列数目比较
1 2 3 1 1 - 0 = 1
1 2 3 4 3 3 - 1 = 2
1 2 3 4 5 6 6 - 3 = 3
1 2 3 4 5 6 10 10 - 6 = 4
1 2 3 4 5 6 7 15 15 - 10 = 5
time : O(n)
space : O(1)
* @param A
* @return
*/
public int numberOfArithmeticSlices(int[] A) {
int cur = 0, res = 0;
for (int i = 2; i < A.length; i++) {
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
cur++;
res += cur;
} else {
cur = 0;
}
}
return res;
}
}