mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加 0001.两数之和.md php版本
This commit is contained in:
@ -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 [];
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user