Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找与目标值相加的所有对_Javascript_Algorithm - Fatal编程技术网

Javascript 查找与目标值相加的所有对

Javascript 查找与目标值相加的所有对,javascript,algorithm,Javascript,Algorithm,我一直在看这个例子,它应该是一种比使用多个循环更快的匹配方法。我见过,但这对我来说毫无意义 有人能帮我把这个分解一下吗?target-arr[i]是用来做什么的 const arr = [7, 0, -4, 5, 2, 3]; const twoSum = (arr, target) => { let map = {} let results = []; for (let i=0; i<arr.length; i++) { if (map[arr[i]] !==

我一直在看这个例子,它应该是一种比使用多个循环更快的匹配方法。我见过,但这对我来说毫无意义

有人能帮我把这个分解一下吗?target-arr[i]是用来做什么的

const arr = [7, 0, -4, 5, 2, 3];

const twoSum = (arr, target) => {
  let map = {}
  let results = [];
  for (let i=0; i<arr.length; i++) {
    if (map[arr[i]] !== undefined) {
      results.push([map[arr[i]], arr[i]])
    } else {
      map[target - arr[i]] = arr[i];
    }
  }
  return results;
}
console.log('twoSum = ', twoSum(arr, 5));
const arr=[7,0,-4,5,2,3];
常量twoSum=(arr,目标)=>{
设map={}
让结果=[];

对于(设i=0;i假设目标为t。给定数组中的值x,您想知道数组中是否存在值t-x,在这种情况下,总和为t-x+x=t

所以你通过数组来标记你在数组中看到x的事实,你在地图中标记了条目t-x。后来当你在数组中遇到t-x时,你检查了地图中的条目t-x,如果它被填充了,那么你就知道你以前看到了x,这意味着你有一对x和t-x。我刚才描述的方式听起来像是在地图中的两个循环数组,但您可以在一个循环中完成这两件事,并且工作原理相同


如果已填充贴图条目,则您以前看到了它的对值,如果未填充,则标记贴图以查看以后是否遇到该对值。

您甚至可以使其更快,而无需存储实际值,因为您正在查找两个值,一个已知,另一个已知

const
arr=[7,0,-4,5,2,3],
twoSum=(arr,目标)=>{
设map={},
结果=[];
for(设i=0;iconsole.log('twoSum=',twoSum(arr,5));
您链接到的解释中似乎有一个错误:它说,“我们的新键/值对是
5:5
。我们的哈希映射现在包含两个条目:
{7:-2,5:5}
”新的键/值(在代码中正确实现)是
5:0

为了理解它是如何工作的,假设我们的数组是
[2,6,3]
,目标是
5
。一旦我们看到
2
,我们想知道数组是否有它的伙伴,其总和是
5

x + 2 = 5
x = 5 - 2
x = 3
因此,我们正在寻找
3
。现在,如果我们知道某个值的键,JavaScript映射对象允许我们高效地检索该值。因此,我们将键设置为
3
——这样,如果我们稍后看到
3
,我们可以快速响应。记住,我们还没有看到
3
。我们只是将键设置为在看到它时快速提醒我们我们已经看到了它的合作伙伴,
2


现在我们继续沿着数组前进。我们经过
6
,但地图中没有键
6
,所以我们将其添加到地图并继续。当我们到达
3
,我们说,“啊哈!”,有
3
的地图提醒我们,我们看到它的合作伙伴加起来等于
5
。我们推送结果,
3
(当前
arr[i]
)和
3
键(
map[arr[i]]
)下存储在映射中的值,这是我们前面看到的
2

算法通过检查当前处理的项和以前看到的项来创建对

因此,它需要一个内存来存储以前看到的项目,这就是为什么
因素映射到解决方案中

让我们分析解决方案中的循环:

  for (let i=0; i<arr.length; i++) {
    if (map[arr[i]] !== undefined) {
      results.push([map[arr[i]], arr[i]])
    } else {
      map[target - arr[i]] = arr[i];
    }
  }

for(让i=0;这是一个简洁的解决方案!我不会想到这一点。谢谢Sean,但这对我来说没有什么意义。t-x+x=t感谢Nina提供了一种更快的方法,但我希望了解我发布的示例如何比其他方法更有效。
  for ( let i = 0; i < arr.length; i++ ) {

    // Any item in the array that's not in the memory
    // 1. should primarily be stored
    // 2. such that it's potential pair is stored as a key that's mapped to a value which is the item
    if ( map[ arr[ i ] ] === undefined ) {
      map[ target - arr[ i ] ] = arr[ i ];

      // Examine pairs only in iterations with odd numbered indices.
      // Why? - For the first iteration, the memory is empty...
      continue;
    }

    // this item’s pair is known, so store the pair in the result list
    results.push( [ map[ arr[ i ] ], arr[ i ] ] );
}