<?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 [];
}
网友评论