Skip to content

Commit f760adb

Browse files
refactor 446
1 parent fc3302f commit f760adb

File tree

1 file changed

+24
-20
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+24
-20
lines changed

src/main/java/com/fishercoder/solutions/_446.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,32 @@ A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the seq
4444
[2,6,10]
4545
*/
4646
public class _446 {
47-
/**reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution*/
48-
public int numberOfArithmeticSlices(int[] A) {
49-
int res = 0;
50-
Map<Integer, Integer>[] map = new Map[A.length];
51-
52-
for (int i = 0; i < A.length; i++) {
53-
map[i] = new HashMap<>(i);
54-
55-
for (int j = 0; j < i; j++) {
56-
long diff = (long) A[i] - A[j];
57-
if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) {
58-
continue;
47+
public static class Solution1 {
48+
/**
49+
* reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution
50+
*/
51+
public int numberOfArithmeticSlices(int[] A) {
52+
int res = 0;
53+
Map<Integer, Integer>[] map = new Map[A.length];
54+
55+
for (int i = 0; i < A.length; i++) {
56+
map[i] = new HashMap<>(i);
57+
58+
for (int j = 0; j < i; j++) {
59+
long diff = (long) A[i] - A[j];
60+
if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) {
61+
continue;
62+
}
63+
64+
int d = (int) diff;
65+
int c1 = map[i].getOrDefault(d, 0);
66+
int c2 = map[j].getOrDefault(d, 0);
67+
res += c2;
68+
map[i].put(d, c1 + c2 + 1);
5969
}
60-
61-
int d = (int) diff;
62-
int c1 = map[i].getOrDefault(d, 0);
63-
int c2 = map[j].getOrDefault(d, 0);
64-
res += c2;
65-
map[i].put(d, c1 + c2 + 1);
6670
}
67-
}
6871

69-
return res;
72+
return res;
73+
}
7074
}
7175
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy