mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
添加 0001.两数之和 PHP版本
This commit is contained in:
@ -224,6 +224,31 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @param Integer $target
|
||||
* @return Integer[]
|
||||
*/
|
||||
function twoSum($nums, $target) {
|
||||
if (count($nums) == 0) {
|
||||
return [];
|
||||
}
|
||||
$table = [];
|
||||
for ($i = 0; $i < count($nums); $i++) {
|
||||
$temp = $target - $nums[$i];
|
||||
if (isset($table[$temp])) {
|
||||
return [$table[$temp], $i];
|
||||
}
|
||||
$table[$nums[$i]] = $i;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user