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

Commitea31c7b

Browse files
refactor 560
1 parente5f030b commitea31c7b

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,32 @@
1717
*/
1818
publicclass_560 {
1919

20-
/**credit: https://discuss.leetcode.com/topic/87850/java-solution-presum-hashmap
21-
* We know the key to solve this problem is SUM[i, j].
22-
* So if we know SUM[0, i - 1] and SUM[0, j],
23-
* then we can easily get SUM[i, j] via (SUM[0, j] - SUM[0, i-1]).
24-
* To achieve this, we just need to go through the array,
25-
* calculate the current sum and save number of all seen PreSum to a HashMap.
26-
*
27-
* Time complexity O(n), Space complexity O(n).*/
28-
publicintsubarraySum(int[]nums,intk) {
29-
Map<Integer,Integer>preSum =newHashMap();
30-
intsum =0;
31-
intresult =0;
32-
preSum.put(0,1);
33-
for (inti =0;i <nums.length;i++) {
34-
sum +=nums[i];
35-
if (preSum.containsKey(sum -k)) {
36-
result +=preSum.get(sum -k);
20+
publicstaticclassSolution1 {
21+
22+
/**
23+
* credit: https://discuss.leetcode.com/topic/87850/java-solution-presum-hashmap
24+
* We know the key to solve this problem is SUM[i, j].
25+
* So if we know SUM[0, i - 1] and SUM[0, j],
26+
* then we can easily get SUM[i, j] via (SUM[0, j] - SUM[0, i-1]).
27+
* To achieve this, we just need to go through the array,
28+
* calculate the current sum and save number of all seen PreSum to a HashMap.
29+
* <p>
30+
* Time complexity O(n), Space complexity O(n).
31+
*/
32+
publicintsubarraySum(int[]nums,intk) {
33+
Map<Integer,Integer>preSum =newHashMap();
34+
intsum =0;
35+
intresult =0;
36+
preSum.put(0,1);
37+
for (inti =0;i <nums.length;i++) {
38+
sum +=nums[i];
39+
if (preSum.containsKey(sum -k)) {
40+
result +=preSum.get(sum -k);
41+
}
42+
preSum.put(sum,preSum.getOrDefault(sum,0) +1);
3743
}
38-
preSum.put(sum,preSum.getOrDefault(sum,0) +1);
44+
returnresult;
3945
}
40-
returnresult;
4146
}
4247

4348
}

‎src/test/java/com/fishercoder/_560Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
* Created by fishercoder on 4/29/17.
1111
*/
1212
publicclass_560Test {
13-
privatestatic_560test;
13+
privatestatic_560.Solution1solution1;
1414
privatestaticintexpected;
1515
privatestaticintactual;
1616
privatestaticint[]nums;
1717
privatestaticintk;
1818

1919
@BeforeClass
2020
publicstaticvoidsetup() {
21-
test =new_560();
21+
solution1 =new_560.Solution1();
2222
}
2323

2424
@Test
2525
publicvoidtest1() {
2626
nums =newint[]{1,1,1};
2727
k =2;
2828
expected =2;
29-
actual =test.subarraySum(nums,k);
29+
actual =solution1.subarraySum(nums,k);
3030
assertEquals(expected,actual);
3131
}
3232
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp