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

Commit74feddd

Browse files
edit 523
1 parent1edd764 commit74feddd

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Your ideas/fixes/algorithms are more than welcome!
110110
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_526.java) | O(n) |O(h) | Medium | Backtracking
111111
|525|[Contiguous Array](https://leetcode.com/problems/contiguous-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_525.java) | O(n) |O(n) | Medium | HashMap
112112
|524|[Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_524.java) | O(n) |O(n) | Medium | Sort
113-
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_523.java)| O(n^2)|O(n)| Medium|
113+
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_523.java) | O(n) |O(1) | Medium| DP
114114
|522|[Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_522.java)| O(x*n^2) (x is average length of strings)|O(1)| Medium|
115115
|521|[Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_521.java)| O(max(x,y)) (x and y are length of strings)|O(1)| Easy|
116116
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_520.java)| O(n)|O(1)| Easy|

‎src/main/java/com/fishercoder/solutions/_523.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
packagecom.fishercoder.solutions;
22

3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
36
/**
7+
* 523. Continuous Subarray Sum
8+
*
49
* Given a list of non-negative numbers and a target integer k,
510
* write a function to check if the array has a continuous subarray of size at least 2
611
* that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
@@ -21,7 +26,29 @@
2126
2227
*/
2328
publicclass_523 {
24-
/**TODO: could be optimized to O(n) time and O(k) space, reference: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space*/
29+
30+
publicbooleancheckSubarraySumOnTimeO1Space(int[]nums,intk) {
31+
Map<Integer,Integer>map =newHashMap<>();
32+
map.put(0, -1);
33+
intsum =0;
34+
for (inti =0;i <nums.length;i++) {
35+
sum +=nums[i];
36+
if (k !=0) {
37+
/**Because if k == 0, sum %= k will throw ArithmeticException.*/
38+
sum %=k;
39+
}
40+
Integerprev =map.get(sum);
41+
if (prev !=null) {
42+
if (i -prev >1) {
43+
/**This makes sure that it has length at least 2*/
44+
returntrue;
45+
}
46+
}else {
47+
map.put(sum,i);
48+
}
49+
}
50+
returnfalse;
51+
}
2552

2653
publicbooleancheckSubarraySum(int[]nums,intk) {
2754
if (nums ==null ||nums.length ==0)returnfalse;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp