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

Commite4e905b

Browse files
Create 523-Continuous-Subarray-Sum.java
1 parent64355b1 commite4e905b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎java/523-Continuous-Subarray-Sum.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//We are basically storing sum%k and storing it in the hashmap and checking it.
2+
//Math logic is that the overall sum will get cancelled out because of modulo
3+
4+
classSolution {
5+
publicbooleancheckSubarraySum(int[]nums,intk) {
6+
HashMap<Integer,Integer>map =newHashMap<>();
7+
map.put(0, -1);
8+
intsum =0;
9+
for (inti =0;i<nums.length;i++) {
10+
sum +=nums[i];
11+
intrem =sum%k;
12+
if (map.containsKey(rem))
13+
if (i -map.get(rem)>=2)
14+
returntrue;
15+
if (!map.containsKey(rem))
16+
map.put(rem,i);
17+
}
18+
returnfalse;
19+
}
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp