From 7f7d1eed323080d1755140484f095b4deec7a08b Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:50:54 -0400 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index f12b5869..a6381eff 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -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)