Updated and Latest (2017)- solution


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

Log in to reply
 

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