# Subarray Sum Zero

## Subarray Sum Zero

## 题目地址

<https://www.lintcode.com/problem/subarray-sum>

<https://leetcode.com/problems/continuous-subarray-sum/>

## 题目描述

```
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.
```

## 代码

```java
public class Solution {
    int n = nums.length;
  ArrayList<Integer> result = new ArrayList<Integer>();
  HashMap<Integer, Integer> map = new HashMap<>();
  map.put(0, -1);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += nums[i];
    if (map.containsKey(sum)) {
      result.add(map.get(sum) + 1);
      result.add(i);
      return result;
    }
    map.put(sum, i);
  }

  return result;
}
```

Approach 2:

```java
public class Solution {
  public int maxSubArray(int[] nums) {
    if (nums == null || nums.isEmpty()) return -1;
    ArrayList<Integer> result = new ArrayList<Integer>();
      HashMap<Integer, Integer> map = new HashMap<>();
    int sum = 0, minSum = 0, maxSub = Integer.MIN_VALUE;
    for (int i = 0; i < n; i++) {
      minSum = Math.min(minSum, sum);
      sum += nums[i];
      if (maxSub < sum - minSum) {
        maxSub = sum - minSum;
        map.put(sum, i);
        result.
      }
    }
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/data-structure/subarray-sum-zero.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
