-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticSlices.java
More file actions
113 lines (98 loc) · 3.33 KB
/
ArithmeticSlices.java
File metadata and controls
113 lines (98 loc) · 3.33 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* ArithmeticSlices
* https://leetcode-cn.com/problems/arithmetic-slices/
* 413. 等差数列划分
* https://leetcode-cn.com/problems/arithmetic-slices/solution/sou-suo-kan-si-jian-dan-dan-shi-yao-kao-me9ax/
* 这个月题目平均错2次, 3次才能pass. 可能技能下降, 或者没有考虑周全.
*
* @author tobin
* @since 2021-08-12
*/
public class ArithmeticSlices {
public static void main(String[] args) {
ArithmeticSlices sol = new ArithmeticSlices();
System.out.println(sol.numberOfArithmeticSlices(new int[]{1, 3, 5, 7, 9}));
System.out.println(sol.numberOfArithmeticSlices(new int[]{1, 3, 5, 7}));
System.out.println(sol.numberOfArithmeticSlices(new int[]{2, 1, 3, 4, 2, 3}));
System.out.println(sol.numberOfArithmeticSlices(new int[]{1, 2, 3, 5, 7}));
}
public int numberOfArithmeticSlices(int[] nums) {
List<Integer> avilables = new LinkedList<>();
// boolean[] flags = new boolean[nums.length]; // bug2: 可以重复取
for (int i = 0; i < nums.length; i++) {
int first = nums[i];
int j = i + 1;
if (j >= nums.length) {
continue;
}
int second = nums[j];
int dist = second - first;
int next = second + dist;
int length = 2;
for (int k = j + 1; k < nums.length; k++) {
if (nums[k] == next) {
length++;
next = next + dist;
} else {
break; // bug1: 题目要求连续
}
}
if (length > 2) {
avilables.add(length);
}
}
int total = 0;
for (int length : avilables) {
total += (length - 2);
}
return total;
}
public int numberOfArithmeticSlices_2(int[] nums) {
List<Integer> avilables = new LinkedList<>();
boolean[] flags = new boolean[nums.length];
for (int i = 0; i < nums.length; i++) {
if (flags[i]) {
continue;
}
int first = nums[i];
for (int j = i + 1; j < nums.length; j++) {
if (flags[j]) {
break;
}
int second = nums[j];
int dist = second - first;
int next = second + dist;
int length = 2;
for (int k = j + 1; k < nums.length; k++) {
if (flags[k]) {
break;
}
if (nums[k] == next) {
flags[k] = true;
length++;
next = next + dist;
} else {
break;
}
}
if (length > 2) {
flags[i] = true;
flags[j] = true;
avilables.add(length);
} else {
break;
}
}
}
int total = 0;
for (int length : avilables) {
for (int range = 3; range <= length; range++) {
total += (length - range + 1);
}
}
return total;
}
}