-
Notifications
You must be signed in to change notification settings - Fork 0
/
413-Arithmetic_Slices.html
66 lines (60 loc) · 2.02 KB
/
413-Arithmetic_Slices.html
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html>
<script>
/**
* @param {number} n
* @return {string[]}
*/
var numberOfArithmeticSlices = function (A) {
let count = 0;
let getCount = function (a, indexA, b, indexB, interval) {
if (indexA === indexB) {
for (let j = 1; j < A.length / 2; j++) {
if (indexA - j >= 0 && indexB + j < A.length) {
if (A[indexA - j] - a === b - A[indexB + j]) {
// console.log([A[indexA - j], a, b, A[indexB + j]]);
count++;
} else {
break;
}
} else {
break;
}
}
}else{
let found = false;
for (let j = 1; j < A.length / 2; j++) {
if (indexA - j >= 0 && indexB + j < A.length) {
if (A[indexA - j] - a === b - A[indexB + j]) {
if(A[indexA - j] - a === a - b || found){
count++;
let found = true;
}
} else {
break;
}
} else {
break;
}
}
}
}
for (let i = 1; i < A.length; i++) {
getCount(A[i], i, A[i], i);
i + 1 < A.length ? getCount(A[i], i, A[i + 1], i + 1, A[i+1]-A[i]) : null;
}
return count;
};
var numberOfArithmeticSlices2 = function(A){
let curr = 0, sum = 0;
for (let i = 2; i < A.length; i++)
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
curr += 1;
sum += curr;
} else {
curr = 0;
}
return sum;
}
numberOfArithmeticSlices([1, 2, 3, 4, 5, 6]);
</script>
</html>