添加 0001.两数之和.md php版本

This commit is contained in:
amdrose
2021-07-02 01:47:13 +08:00
parent 0f38eb9065
commit c47bafa412

View File

@ -187,7 +187,23 @@ var twoSum = function (nums, target) {
};
```
php
```php
function twoSum(array $nums, int $target): array
{
for ($i = 0; $i < count($nums);$i++) {
// 计算剩下的数
$residue = $target - $nums[$i];
// 匹配的index有则返回index 无则返回false
$match_index = array_search($residue, $nums);
if ($match_index !== false && $match_index != $i) {
return array($i, $match_index);
}
}
return [];
}
```
-----------------------