Skip to content

Commit ea31c7b

Browse files
refactor 560
1 parent e5f030b commit ea31c7b

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
public class _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-
public int subarraySum(int[] nums, int k) {
29-
Map<Integer, Integer> preSum = new HashMap();
30-
int sum = 0;
31-
int result = 0;
32-
preSum.put(0, 1);
33-
for (int i = 0; i < nums.length; i++) {
34-
sum += nums[i];
35-
if (preSum.containsKey(sum - k)) {
36-
result += preSum.get(sum - k);
20+
public static class Solution1 {
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+
public int subarraySum(int[] nums, int k) {
33+
Map<Integer, Integer> preSum = new HashMap();
34+
int sum = 0;
35+
int result = 0;
36+
preSum.put(0, 1);
37+
for (int i = 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+
return result;
3945
}
40-
return result;
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
public class _560Test {
13-
private static _560 test;
13+
private static _560.Solution1 solution1;
1414
private static int expected;
1515
private static int actual;
1616
private static int[] nums;
1717
private static int k;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _560();
21+
solution1 = new _560.Solution1();
2222
}
2323

2424
@Test
2525
public void test1() {
2626
nums = new int[]{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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy