Java O(N) - Beats 100%


  • 0
    A
    public int[] twoSum(int[] nums, int target) {
    
        Map<Integer, Integer> track = new HashMap<>();
    
        for (int i = 0; i < nums.length; i++) {
            int j = track.getOrDefault(target - nums[i], -1);
            if (j != -1) return new int[]{j, i};
            track.put(nums[i], i);
        }
    
        return null;
    }

Log in to reply
 

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