Repository navigation
Expand file tree
/
Copy pathSubarray Sum.java
More file actions
47 lines (43 loc) · 1.62 KB
/
Copy pathSubarray Sum.java
File metadata and controls
47 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.
Example
Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].
Note
There is at least one subarray that it's sum equals to zero.
Tags Expand
Subarray Hash Table
Thougths:
Record the sum from (0 ~ a).
Check sum on each index i, when found an existing sum in the hashMap, we are done.
Reason:
If adding all the numbers together, for example if sum[0 ~ a] = -3, ... sum[0 - b] = -3 again, a<b
That means from a ~ b, there is not change: that is, sum[a - b] = 0.
As result, hashmap.get(a)+1 will be the satrting index, and b will be ending index.
*/
public class Solution {
/**
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number
* and the index of the last number
*/
public ArrayList<Integer> subarraySum(int[] nums) {
ArrayList<Integer> rst = new ArrayList<Integer>();
if (nums == null || nums.length == 0) {
return rst;
}
int sum = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, -1);
//we know that sub-array (a,b) has zero sum if SUM(0 ... a-1) = SUM(0 ... b)
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum)) {
rst.add(map.get(sum) + 1);
rst.add(i);
return rst;
}
map.put(sum, i);
}//for
return rst;
}
}