您的位置:首页 > 其它

找出数组中和为0的子数组

2013-01-08 23:40 134 查看
private static void subArraySumsZero()
{
int [] seed = new int[] {1,2,3,4,-9,6,7,-8,1,9};
int currSum = 0;

//key: is the currSum  value: the index
HashMap<Integer, Integer> sumMap = new HashMap<Integer, Integer>();

for(int i = 0 ; i < seed.length ; i ++)
{
currSum += seed[i];
if(currSum == 0)
{
System.out.println("subset : { 0 - " + i + " }");
}
else if(sumMap.get(currSum) != null)
{
System.out.println("subset : { " + (sumMap.get(currSum) + 1) + " - " + i + " }");
sumMap.put(currSum, i);
}
else
{
sumMap.put(currSum, i);
}
System.out.println("HASH MAP HAS: " + sumMap);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: