给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位
样例
给出 [-3, 1, 2, -3, 4]
,返回[0, 2]
或者 [1, 3]
.
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) {
// write your code here
ArrayList<Integer> arrayList = new ArrayList<>();
boolean b = false;
for(int i=0;i<nums.length;i++)
{
int num = 0;
for(int j=i;j<nums.length;j++)
{
num += nums[j];
// System.out.println(num);
if(num == 0)
{
arrayList.add(i);
arrayList.add(j);
b = true;
break;
}
}
if(b == true)
break;
}
return arrayList;
}
}