day01:两数之和(Java)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map map = new HashMap<>();
        for (int i = 0; i <= nums.length-1; i++) {
            if (map.containsKey(target - nums[i])) 
                return new int[]{i, map.get(target - nums[i])};
             map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}

 

你可能感兴趣的:(leetcode)