简体   繁体   English

两个总和数组 - 蛮力 - 我做错了什么?

[英]Two Sum Array - Brute Force - what am I doing wrong?

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

twoSum([3, 3], 6)

expected output: [0, 1] received: [0, 2]预计 output:[0, 1] 收到:[0, 2]

description: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.描述:给定一个整数数组 nums 和一个 integer 目标,返回两个数字的索引,使它们加起来等于目标。

@lucumt already pointed out that you should leave out two lines of your code. @lucumt 已经指出您应该省略两行代码。 However, a slightly more conventional way of expressing your function would involve for() loops as they would make the code slightly more readable than the chosen while() construct.然而,一种稍微更传统的表达 function 的方法将涉及for()循环,因为它们会使代码比选择的while()结构更具可读性。 The inner loop should also always start at index+1 , see below:内部循环也应该始终从index+1开始,见下文:

 var twoSum = function(nums, target) { const res=[]; for (let index=0; index <nums.length; index++) { for (let i=index+1;i <nums.length; i++) { if (nums[index] + nums[i] === target ) res.push([index, i]); } } return res } console.log(twoSum([3, 3], 6)) // [[0,1]] // second example: console.log(twoSum([1,6,4,7,3,2,8,3], 6)) // [[2,5],[4,7]]

My snippet will return all possible sums in an array of arrays. If you only want the first solution, simply do twoSum(arr,targetNum)[0] .我的代码片段将返回 arrays 数组中所有可能的总和。如果您只想要第一个解决方案,只需执行twoSum(arr,targetNum)[0]

You need to remove below code when you found the matched result找到匹配结果后,您需要删除以下代码

    if (nums[index] === nums[i]) {
        i = i + 1
    }

 var twoSum = function(nums, target) { let index = 0 let i = 1 while (index <= nums.length - 1) { while (i <= nums.length - 1) { if (nums[index] + nums[i] === target) { return([index, i]) } else { i = i + 1 } } index = index + 1 i = 0 } }; console.log(twoSum([3, 2, 0,1,4], 6))

Just remove this block.只需删除此块。

                    if (nums[index] === nums[i]) {
                        i = i + 1
                    }

BTW, why you write this code from the start.顺便说一句,为什么你从一开始就写这段代码。 You are not getting all the possible numbers.你没有得到所有可能的数字。 No matter how long the input array is, you just return 2 indexes.无论输入数组有多长,您只返回 2 个索引。

let index = 0
while (index <= nums.length - 1) {
        let i = 0
        while (i++ <= nums.length - 1) {
            if (index!==i && nums[index] + nums[i] === target) {
              return([index, i])
            }
        }
    index++
}

You can use for-loop with the same logic because it looks more simple.您可以使用具有相同逻辑的 for 循环,因为它看起来更简单。

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

console.log(twoSum([3, 3], 6));

Skip that "if" statement above it will increase your i's value for this case.跳过上面的“if”语句会增加你的 i 在这种情况下的价值。

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

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