-
Notifications
You must be signed in to change notification settings - Fork 0
/
subarray-sum.cpp
38 lines (31 loc) · 936 Bytes
/
subarray-sum.cpp
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
class Solution {
public:
/**
* @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
*/
vector<int> subarraySum(vector<int> nums){
if (nums.size() <= 1) {
vector<int> result = { 0, 0 };
return result;
};
vector<int> sums(nums);
sums.insert(sums.begin(), 0);
for (int i=1; i<sums.size(); i++) {
sums[i] += sums[i-1];
}
for (int i=1; i<sums.size(); i++) {
if (sums[i] == 0) {
vector<int> result = { i-1, i-1 };
}
for (int j=i; j<sums.size(); j++) {
if (sums[j] - sums[i-1] == 0) {
vector<int> result = { i-1, j-1 };
return result;
}
}
}
return vector<int>();
}
};