3ms Java Solution beats over 91%


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

Log in to reply
 

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