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;
    }

猜你喜欢

转载自www.cnblogs.com/whyaza/p/10656507.html