提交代码
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
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");
}
}
本题最简易方法:暴力法
提交代码的思路:是用HashMap存储每个元素,每次存入元素的同时,判断是否存在target-x。
原文:https://www.cnblogs.com/chenshaowei/p/11850114.html