Easy understand java solution


  • 4
    C
    public int[] twoSum(int[] nums, int target){
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < nums.length; i++){
            map.put(nums[i],i+1);
        }
        for(int i = 0; i < nums.length; i++){
            if (map.containsKey(target-nums[i]) && map.get(target-nums[i]) > i+1 )
                return new int[] {i+1, map.get(target-nums[i])};
        }
        return new int[2];
    }

  • 0
    X
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.