# 两数之和为k

java

```
```

python

```python
'''
找出数组中和为某个数的两个数的序号
思路：和为k等价与k-a=b，建立字典，遍历每个元素ai
如果k-ai不存在字典中就记下ai的位置i来，到下一个数如果k-aj=ai在字典中，那么结果就是(i,j)
'''


class Solution:
    def call(self, nums, target):
        hashset = {}
        for i, m in enumerate(nums):
            if target - m not in hashset:
                hashset[m] = i
            else:
                return (hashset[target - m] , i )


s = Solution()
print(s.call([2, 5, 7, 11, 15], 9))
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/zhi-shang-lei/13-liang-shu-zhi-he-wei-k.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.
