Java. Faster hashmap and simple.


  • 5
    I

    public class Solution {

    public int[] twoSum(int[] nums, int target) {
    
        HashMap<Integer, Integer> b = new HashMap<Integer, Integer>(nums.length<<1);
    
        for(int i = 0; i < nums.length; i++){
    
            if(b.containsKey(nums[i])){
    
                return new int[]{b.get(nums[i])+1, i+1};
    
            }else{
    
               b.put(target - nums[i], i); 
    
            }
    
        }
    
        return new int[2];
    
    }
    

    }


Log in to reply
 

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