7ms Java Solution


  • 0
    P
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> hs=new HashMap<Integer,Integer>();
    
    	int[] n=new int[2];
    	for(int i=0;i<nums.length;i++){
    		hs.put(nums[i],i);
    	}
    	
    	for(int i=0;i<nums.length;i++){
    		int val=target-nums[i];
    		 
    		if(hs.containsKey(val)){
    			int subVal=hs.get(val);
    			if(i!=subVal){
    				n[0]=i;
    				n[1]=hs.get(val);
    				return n;
    			}
    			
    		}
    	}
    	return n;
    }

Log in to reply
 

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