题目描述
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
Python解法
解题思路:
1.循环遍历数组中的每个值num,用目标值target减去num得到另一个值another_num
2.判断another_num是否存在于hashmap字典
- 如another_num不存在于hashmap字典中,将num和num的下标存放至hashmap字典,继续遍历
- 如存在则返回another_num在hashmap字典中的下标和当前遍历的num的下标Index
Step | index | num | another_num | hashmap |
1 | 0 | 4 | 13-4=9 | {4:0} |
2 | 1 | 5 | 13-5=8 | {4:0,5: |