其他分享
首页 > 其他分享> > 两数之和

两数之和

作者:互联网

$array = [2,5,7,9,11];
$target = 11;

function twoSums($array, $target) {
$count = count($array);
for ($i=0;$i<$count;$i++) {
$diff = $target - $array[$i];
$newArray = $array;
unset($newArray[$i]);

if (in_array($diff, $newArray)) {
return [$i, array_search($diff, $newArray)];
}
}
}

$a=twoSums($array, $target);
var_dump($a);

标签:11,count,target,newArray,diff,array,两数
来源: https://www.cnblogs.com/jianzhaojing/p/12759269.html