O(n) Java


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