两数之和

$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);
上一篇:JS数组对象的去重和排序操作


下一篇:C#练习题答案: 数字对【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战