leetcode 1. Two Sum [java]

注意点:

  1. HashMap<Integer, Integer>
  2. return new int[]{};
  3. 3 2 4 target:6
  4. 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;
}
上一篇:Units of CSS


下一篇:LeetCode #139. Word Break C#