@@ -44,28 +44,32 @@ A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the seq
44
44
[2,6,10]
45
45
*/
46
46
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 );
59
69
}
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 );
66
70
}
67
- }
68
71
69
- return res ;
72
+ return res ;
73
+ }
70
74
}
71
75
}
0 commit comments