-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0138.Subarray_Sum.py
More file actions
63 lines (51 loc) · 1.77 KB
/
0138.Subarray_Sum.py
File metadata and controls
63 lines (51 loc) · 1.77 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Description
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.
There is at least one subarray that it's sum equals to zero.
Example
Example 1:
Input: [-3, 1, 2, -3, 4]
Output: [0, 2] or [1, 3].
Explanation: return anyone that the sum is 0.
*/
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
"""
def subarraySum(self, nums):
# write your code here
prefix_hash = {0: -1} #0 is the sum
prefix_sum = 0
count = len(nums)
for i in range(count):
prefix_sum += nums[i]
if prefix_sum in prefix_hash:
return prefix_hash[prefix_sum] + 1, i
else:
prefix_hash[prefix_sum] = i
return None
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 List<Integer> subarraySum(int[] nums) {
// write your code here
int len = nums.length;
List<Integer> result = new ArrayList<Integer>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, -1);
int sum = 0;
for (int i = 0; i < len; i++) {
sum += nums[i];
if (map.containsKey(sum)) {
result.add(map.get(sum) + 1); // 思考下[-3, 1, 2, -3, 4],输出[1, 3],思考下就知道为什么要+1了
result.add(i);
return result;
}
map.put(sum, i);
}
return result;
}
}