# Subarray Sum Zero

## Subarray Sum Zero

## 题目地址

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

## 题目描述

```
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.
```

## 代码

### Approach #1 HashMap, 查找两个sum

```java
public class Solution {
  public ArrayList<Integer> subarraySum (int[] nums) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
    int sum = 0;
    for (int i = 0; i < nums.length; i++) {
      sum += nums[i];
      if (sum == 0) {
        result.add(0);
        result.add(i);
        return result;
      }

      if (hashMap.containsKey(sum)) {
        int index = hashMap.get(sum);
        result.add(index);
        result.add(i);
        return result;
      }

      hashMap.put(sum, i);
    }

    return 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/array/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.
