Merge pull request #1563 from whosphp/master

修正 0001.两数之和 php版本实现
This commit is contained in:
程序员Carl
2022-08-18 09:16:36 +08:00
committed by GitHub

View File

@ -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 [];