mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
@ -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 [];
|
||||||
|
Reference in New Issue
Block a user