两数之和

2022-07-14  本文已影响0人  Newzer
<?php
$num = [3,2,4];
$target = 6;
print_r(twoSum($num, $target));

function twoSum($num, $target) {
    $count = count($num);

    $map = array();
    for ($i = 0; $i < $count; $i ++) {
        $other = $target - $num[$i];
        if (isset($map[$other])){
            return [$map[$other],$i];
        }else {
            $map[$num[$i]] = $i;
        }
    }
    return [];
}
上一篇 下一篇

猜你喜欢

热点阅读