Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9330f0d

Browse files
add 446
1 parent6cf23a6 commit9330f0d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Your ideas/fixes/algorithms are more than welcome!
170170
|449|[Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_449.java)| O(n)|O(h) | Medium| BFS
171171
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_448.java)| O(n)|O(1) | Easy| Array, HashMap
172172
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_447.java)| O(n^2)|O(n) | Easy| HashMap
173+
|446|[Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_446.java)| O(n^2)|O(n^2) | Hard| DP
173174
|445|[Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_445.java)| O(max(m,n)|O(max(m,n)) | Medium| Stack, LinkedList
174175
|444|[Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_444.java)| O(n)|O(n) | Medium| Topological Sort, Graph
175176
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_442.java)| O(n)|O(1) | Medium| Array
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
/**
7+
* 446. Arithmetic Slices II - Subsequence
8+
*
9+
* A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
10+
11+
For example, these are arithmetic sequences:
12+
13+
1, 3, 5, 7, 9
14+
7, 7, 7, 7
15+
3, -1, -5, -9
16+
17+
The following sequence is not arithmetic.
18+
19+
1, 1, 2, 5, 7
20+
21+
A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.
22+
23+
A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.
24+
25+
The function should return the number of arithmetic subsequence slices in the array A.
26+
27+
The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.
28+
29+
30+
Example:
31+
32+
Input: [2, 4, 6, 8, 10]
33+
34+
Output: 7
35+
36+
Explanation:
37+
All arithmetic subsequence slices are:
38+
[2,4,6]
39+
[4,6,8]
40+
[6,8,10]
41+
[2,4,6,8]
42+
[4,6,8,10]
43+
[2,4,6,8,10]
44+
[2,6,10]
45+
*/
46+
publicclass_446 {
47+
/**reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution*/
48+
publicintnumberOfArithmeticSlices(int[]A) {
49+
intres =0;
50+
Map<Integer,Integer>[]map =newMap[A.length];
51+
52+
for (inti =0;i <A.length;i++) {
53+
map[i] =newHashMap<>(i);
54+
55+
for (intj =0;j <i;j++) {
56+
longdiff = (long)A[i] -A[j];
57+
if (diff <=Integer.MIN_VALUE ||diff >Integer.MAX_VALUE)continue;
58+
59+
intd = (int)diff;
60+
intc1 =map[i].getOrDefault(d,0);
61+
intc2 =map[j].getOrDefault(d,0);
62+
res +=c2;
63+
map[i].put(d,c1 +c2 +1);
64+
}
65+
}
66+
67+
returnres;
68+
}
69+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp