题目
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
解题思路
- 把target-nums[i]作为key值放入hashmap
- 遍历数组检查hashmap中是否已经存在相加等一target值
- 存在取出
- 不存在将自己存入hashmap继续遍历
代码
1 |
public int[] twoSum(int[] nums, int target) { |
近期评论