Accepted Java solution - 7ms


  • 0
    S

    Accepted Java solution.

    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                Integer index = map.get(nums[i]);
                if (index != null) {
                    return new int[]{index, i};
                }
                map.put(target - 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.