Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8e9211e commit be90aecCopy full SHA for be90aec
Medium/Arithmetic Slices.java
@@ -1,17 +1,15 @@
1
class Solution {
2
public int numberOfArithmeticSlices(int[] A) {
3
- int count = 0;
4
- for (int i = 0; i < A.length - 2; i++) {
+ int counter = 0;
+ int n = A.length;
5
+ for (int i = 0; i < n - 2; i++) {
6
int diff = A[i + 1] - A[i];
- for (int j = i + 2; j < A.length; j++) {
7
- if (A[j] - A[j - 1] == diff) {
8
- count++;
9
- }
10
- else {
11
- break;
12
+ int nextIdx = i + 2;
+ while (nextIdx < n && A[nextIdx] - A[nextIdx - 1] == diff) {
+ nextIdx++;
13
}
+ counter += nextIdx - i - 2;
14
15
- return count;
+ return counter;
16
17
0 commit comments