修正 0001.两数之和 php版本实现
This commit is contained in:
parent
984576943a
commit
ddf0530d44
|
|
@ -263,13 +263,15 @@ php
|
||||||
```php
|
```php
|
||||||
function twoSum(array $nums, int $target): array
|
function twoSum(array $nums, int $target): array
|
||||||
{
|
{
|
||||||
for ($i = 0; $i < count($nums);$i++) {
|
$map = [];
|
||||||
// 计算剩下的数
|
foreach($nums as $i => $num) {
|
||||||
$residue = $target - $nums[$i];
|
if (isset($map[$target - $num])) {
|
||||||
// 匹配的index,有则返回index, 无则返回false
|
return [
|
||||||
$match_index = array_search($residue, $nums);
|
$i,
|
||||||
if ($match_index !== false && $match_index != $i) {
|
$map[$target - $num]
|
||||||
return array($i, $match_index);
|
];
|
||||||
|
} else {
|
||||||
|
$map[$num] = $i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue