Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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,问题 给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标 您可以假设每个输入都有一个解决方案,并且不能两次使用同一个元素 但是,我特别想用findIndex()来解决这个问题。问题是我们需要两个指数相加!不止一个 函数twoSum(arr,目标){ 设relevantArr=[]; 设newArr=arr.slice(0,); 功能测试(){ 返回newArr.findIndex(已添加); } 新增功能(a、b){ a+b==目标; } 返回相关ARR } log(twoSum

问题

给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标

您可以假设每个输入都有一个解决方案,并且不能两次使用同一个元素

但是,我特别想用findIndex()来解决这个问题。问题是我们需要两个指数相加!不止一个

函数twoSum(arr,目标){
设relevantArr=[];
设newArr=arr.slice(0,);
功能测试(){
返回newArr.findIndex(已添加);
}
新增功能(a、b){
a+b==目标;
}
返回相关ARR
}

log(twoSum([2,7,11,15],9))
您可以循环数组项并尝试找到其补码以获得目标。如果未找到补码,
findIndex
返回-1,然后检查下一个补码

function twoSum(arr, target){
    for(let i = 0, len = arr.length ; i < len ; ++i)
    {
        // try to find an item so that its sum with arr[i] is equal to the target
        let j = arr.findIndex(item => item + arr[i] == target); 
        if(j != -1)
        {
            // we found the element, we can return the answer
            return [i, j];
        }
    }
}
console.log(twoSum([2, 7, 11, 15], 9)); // [0,1]
console.log(twoSum([2, 7, 11, 15], 18)); // [1,2]
函数twoSum(arr,目标){
for(设i=0,len=arr.length;iitem+arr[i]==target);
如果(j!=-1)
{
//我们找到了元素,我们可以返回答案
返回[i,j];
}
}
}
log(twoSum([2,7,11,15,9]);//[0,1]
log(twoSum([2,7,11,15,18]);//[1,2]

您可以循环数组项并尝试找到其补码以获得目标。如果未找到补码,
findIndex
返回-1,然后检查下一个补码

function twoSum(arr, target){
    for(let i = 0, len = arr.length ; i < len ; ++i)
    {
        // try to find an item so that its sum with arr[i] is equal to the target
        let j = arr.findIndex(item => item + arr[i] == target); 
        if(j != -1)
        {
            // we found the element, we can return the answer
            return [i, j];
        }
    }
}
console.log(twoSum([2, 7, 11, 15], 9)); // [0,1]
console.log(twoSum([2, 7, 11, 15], 18)); // [1,2]
函数twoSum(arr,目标){
for(设i=0,len=arr.length;iitem+arr[i]==target);
如果(j!=-1)
{
//我们找到了元素,我们可以返回答案
返回[i,j];
}
}
}
log(twoSum([2,7,11,15,9]);//[0,1]
log(twoSum([2,7,11,15,18]);//[1,2]
事实上,如果您想使用
findIndex
,那么它的返回值永远不够,因为它只是一个索引。您可以使用“副作用”并使用另一个变量来存储第二个索引。对于查找第二个索引,不要再次使用
findIndex
,因为这太过分了。使用
indexOf
,确保它只扫描第一个索引之后的数组部分(使用
indexOf
的第二个参数):

函数twoSum(arr,目标){
设j=-1;//需要通过以下函数访问:
新增功能(val,i){
//修改此函数范围之外的变量:
j=arr.indexOf(target-val,i+1);
返回j>=0;//如果成功则返回true
}
//findIndex将修改j,我们将其添加到该对中
return[arr.findIndex(added),j];//未找到时将为[-1,-1]
}
log(twoSum([2,7,11,15,9]);//建立
log(twoSum([2,7,11,15,100]);//找不到
事实上,如果要使用
findIndex
,那么它的返回值永远都不够,因为它只是一个索引。您可以使用“副作用”并使用另一个变量来存储第二个索引。对于查找第二个索引,不要再次使用
findIndex
,因为这太过分了。使用
indexOf
,确保它只扫描第一个索引之后的数组部分(使用
indexOf
的第二个参数):

函数twoSum(arr,目标){
设j=-1;//需要通过以下函数访问:
新增功能(val,i){
//修改此函数范围之外的变量:
j=arr.indexOf(target-val,i+1);
返回j>=0;//如果成功则返回true
}
//findIndex将修改j,我们将其添加到该对中
return[arr.findIndex(added),j];//未找到时将为[-1,-1]
}
log(twoSum([2,7,11,15,9]);//建立
log(twoSum([2,7,11,15,100]);//未找到