给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置
样例
样例 1:
输入: [-3, 1, 2, -3, 4]
输出: [0,2] 或 [1,3]
样例解释: 返回任意一段和为0的区间即可。
样例 2:
输入: [-3, 1, -4, 2, -3, 4]
输出: [1,5]
注意事项
至少有一个子数组的和为 0
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) {
// write your code here
vector<int> result;
int len=nums.size();
bool judge=false;
for(int i=0;i<len;i++)
{
int sum=nums[i],num1=i;
if(sum==0)
{
result.push_back(num1);
result.push_back(num1);
return result;
}
else
{
for (int j=i+1;j<len;j++)
{
int num2=j;
sum+=nums[j];
if(sum==0)
{
result.push_back(num1);
result.push_back(num2);
judge=true;
return result;
break;
}
}
}
if(judge) break;
}
}
};```