Leetcode1:两数之和
程序员文章站
2022-04-04 09:53:00
...
题目描述
思路分析
代码实现
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target-nums[i])) {
return new int[] {map.get(target-nums[i]),i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}