Java O(n) solution


  • 0
    M
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> diffs = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                diffs.put(target-nums[i], i);
            }
            for (int i = 0; i < nums.length; i++) {
                if (diffs.containsKey(nums[i]) && i != diffs.get(nums[i])) {
                    return new int[] {i+1, diffs.get(nums[i])+1};
                }
            }
            return new int[0];
        }
    }
    

Log in to reply
 

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