简体   繁体   English

Javascript 中的两个 Sum Leetcode - 代码看起来正确但 Leetcode 说它是错误的

[英]Two Sum Leetcode in Javascript - code looks correct but Leetcode says it's wrong

I'm working on the 'Two Sum' problem in Leetcode .我正在研究Leetcode 中“Two Sum”问题

I'm sure this code is correct, I've tested it in Repl and it looks correct there, but Leetcode is giving me an error.我确定这段代码是正确的,我已经在 Repl 中测试过它并且在那里看起来是正确的,但是 Leetcode 给了我一个错误。

Here's my code:这是我的代码:

var arr = [];

var twoSum = function(nums, target) {
   for(var i = 0; i < nums.length; i++){
        for(var j = i+1; j < nums.length; j++){
            console.log(nums[i] + ', ' + nums[j]);
            var tot = nums[i] + nums[j];        
            if(tot === target){
                arr.push(i,j);
                console.log(arr);
                return arr;
            }     
         }         
   }
};

//var a = [2, 7, 11, 15];
//var b = 9;
var a = [2, 3, 4];
var b = 6;

twoSum(a, b);

The error I'm getting is as follows:我得到的错误如下:

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

Why is it expecting [1, 2] ?为什么它期待[1, 2] Surely it should expect [0, 1] in this case, and then why is my code adding to the arr array twice?在这种情况下,它当然应该期望[0, 1] ,然后为什么我的代码两次添加到 arr 数组? It looks like a bug to me...对我来说这看起来像是一个错误......

Note: I see there's many posts about this problem on Leetcode, but none address the specific issue I have run into in Javascript.注意:我看到 Leetcode 上有很多关于这个问题的帖子,但没有一个解决我在 Javascript 中遇到的具体问题。

Why is it expecting [1, 2]?为什么它期待 [1, 2]?

Because 2 + 4 = 6因为 2 + 4 = 6

Surely it should expect [0, 1] in this case在这种情况下,它当然应该期望 [0, 1]

No, because 3 + 2 = 5不,因为 3 + 2 = 5

and then why is my code adding to the arr array twice?然后为什么我的代码两次添加到 arr 数组?

Because you declared the array outside of the function.因为您在函数外部声明了数组。 It is being re-used for every call to the function.每次调用该函数时都会重新使用它。 Move the array declaration into your twoSum function or even better: Simply return [i, j] instead of push ing into the empty array.将数组声明移动到您的twoSum函数中,甚至更好:只需return [i, j]而不是push到空数组中。

Here is another solution you can try...这是您可以尝试的另一种解决方案...

var twoSum = function(nums, target) {
let map = {};
for (let i = 0; i < nums.length; i++) {
  let compliment = target - nums[i];
  if (map[compliment]) {
    return [(map[compliment] - 1), i];
  } else {
    map[nums[i]] = i + 1;
    }
   }
 };

 twoSum([2, 3, 4],6);

Click Here to RUN点击这里运行

Here is an optimum solution这是一个最佳解决方案

 /** * @param {number[]} nums * @param {number} target * @return {number[]} */ function twoSum(nums, target) { const numsObjs = {}; // create nums obj with value as key and index as value eg: [2,7,11,15] => {2: 0, 7: 1, 11: 2, 15: 3} for (let i = 0; i < nums.length; i++) { const currentValue = nums[i]; if (target - currentValue in numsObjs) { return [i, numsObjs[target - currentValue]]; } numsObjs[nums[i]] = i; } return [-1, -1]; } console.log(twoSum([2, 7, 11, 15], 9))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM