Here is my Java solution with HashMap in O(n)


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

  • 0
    M

    hello suntao i'm yuanba


Log in to reply
 

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