# 两数之和

解法一，暴力枚举法：

```java
public int[] byEnumerate(int[] nums, int target){
    int[] index = new int[2];

    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] == target){
                index[0] = i;
                index[1] = j;
                return index;
            }
        }
    }
    throw new IllegalArgumentException("No two sum solution");
}
```

解法二，哈希映射法：

```java
public int[] byHashMap(int[] nums, int target){
    Map<Integer, Integer> hashMap = new HashMap<>(nums.length);
    for (int i = 0; i < nums.length; i++) {
        Integer x = target - nums[i];

        if (hashMap.containsKey(x)){
            return new int[]{hashMap.get(x), i};
        }
        hashMap.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}
```


---

# 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://deecyn.gitbook.io/notes/algorithms/leetcode/two-sum.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.
