问题描述:
给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
注意事项
There is at least one subarray that it's sum equals to zero.
给出 [-3, 1, 2, -3, 4]
,返回[0, 2]
或者 [1, 3]
.
找到和为零的元素的初末位置,插入向量中。
代码:
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){
vector<int>v;
int n=nums.size();
int j=0;
int m=0;
while(j<n){
int sum=0;
for(int i=j;i<n;i++)
{
sum+=nums[i];
if(sum==0)
{
v.push_back(j);
v.push_back(i);
m++;
break;
}
}
if(m!=0) break;
j++;
}
return v;
// write your code here
}
};
感想:
注意和为零的初末位置只返回一个