leetcode 1. Two Sum [java]
注意点:
- HashMap<Integer, Integer>
- return new int[]{};
- 3 2 4 target:6
- return null;
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<>();
for(int i = 0; i < nums.length; ++i){
if(m.containsKey(target - nums[i]))
return new int[]{m.get(target - nums[i]), i};
m.put(nums[i], i);
}
return null;
}