Two sum result not correct for [3,2,4], 6?


  • 0
    B

    Here is my code in JS:

    var twoSum = function(nums, target) {
      var tempMap ={};
      for(var i = 0; i< nums.length; i++){
        tempMap[nums[i]] = true;
      }
      for(var i = 0; i< nums.length; i++){
        if(target-nums[i] !== nums[i] && tempMap[target-nums[i]]){
          return [nums[i], target - nums[i]];
        }
      }
    };
    

    However, I got wrong answer:

    Input: [3,2,4], 6 Output: [2,4] Expected: [2,3]

    [2,3] shouldn't be the right answer. Can someone help me figure out? Thanks.


  • 1
    L

    return index,not value, [2,3] is index of [2,4]


  • 0
    B

    Oh! Got it. Thanks for clarification.


Log in to reply
 

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