Accepted Java solution


  • 0
    R
    public class Solution {
        public int[] twoSum(int[] nums, int target) {
            
           Map<Integer,Integer> twoSumMap  = new HashMap<Integer,Integer>();
           
           int[] result = new int[2];
           
           for(int i=0;i<nums.length;i++){
               if(twoSumMap.get(target - nums[i])!=null){
                   result[0]=twoSumMap.get(target - nums[i]);
                   result[1]=i;
                   break;
               }else{
                   twoSumMap.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.