From b4c61303477091f537714935db04bb973fbddf27 Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:41:55 -0400 Subject: [PATCH 1/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20PHP=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 555dad03..c4f187e8 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -331,8 +331,32 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? { } ``` - - +PHP: +```php +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ + // 虚拟头+双指针 +func removeElements(head *ListNode, val int) *ListNode { + dummyHead := &ListNode{} + dummyHead.Next = head + pred := dummyHead + cur := head + for cur != nil { + if cur.Val == val { + pred.Next = cur.Next + } else { + pred = cur + } + cur = cur.Next + } + return dummyHead.Next +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 68710ed0066d3b3d9cbece33d116fa7fa3b6ad73 Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:47:00 -0400 Subject: [PATCH 2/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 1b6b3109..3416ac06 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -221,6 +221,41 @@ func isAnagram(_ s: String, _ t: String) -> Bool { } ``` +PHP: +```php +class Solution { + /** + * @param String $s + * @param String $t + * @return Boolean + */ + function isAnagram($s, $t) { + if (strlen($s) != strlen($t)) { + return false; + } + $table = []; + for ($i = 0; $i < strlen($s); $i++) { + if (!isset($table[$s[$i]])) { + $table[$s[$i]] = 1; + } else { + $table[$s[$i]]++; + } + if (!isset($table[$t[$i]])) { + $table[$t[$i]] = -1; + } else { + $table[$t[$i]]--; + } + } + foreach ($table as $record) { + if ($record != 0) { + return false; + } + } + return true; + } +} +``` + ## 相关题目 * 383.赎金信 From 4a78d79db07f72daee8ef0086830f5b8a947d517 Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:48:46 -0400 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200349.=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20PHP?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 752eee51..0cbdf85f 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -209,6 +209,35 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { } ``` +PHP: +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersection($nums1, $nums2) { + if (count($nums1) == 0 || count($nums2) == 0) { + return []; + } + $counts = []; + $res = []; + foreach ($nums1 as $num) { + $counts[$num] = 1; + } + foreach ($nums2 as $num) { + if (isset($counts[$num])) { + $res[] = $num; + } + unset($counts[$num]); + } + + return $res; + } +} +``` + ## 相关题目 * 350.两个数组的交集 II From 485f6a806a712356fb3112b8522149650bc2756c Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:49:37 -0400 Subject: [PATCH 4/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index e6365c71..710c824d 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -223,6 +223,37 @@ func isHappy(_ n: Int) -> Bool { } ``` +PHP: +```php +class Solution { + /** + * @param Integer $n + * @return Boolean + */ + function isHappy($n) { + // use a set to record sum + // whenever there is a duplicated, stop + // == 1 return true, else false + $table = []; + while ($n != 1 && !isset($table[$n])) { + $table[$n] = 1; + $n = self::getNextN($n); + } + return $n == 1; + } + + function getNextN(int $n) { + $res = 0; + while ($n > 0) { + $temp = $n % 10; + $res += $temp * $temp; + $n = floor($n / 10); + } + return $res; + } +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 7f7d1eed323080d1755140484f095b4deec7a08b Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:50:54 -0400 Subject: [PATCH 5/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=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) From 80eb43f36d73826d8131b1febe95d8c33743083a Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:51:43 -0400 Subject: [PATCH 6/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200454.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 4e61dc2f..9fe01ad1 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -220,7 +220,37 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) { }; ``` - +PHP: +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @param Integer[] $nums3 + * @param Integer[] $nums4 + * @return Integer + */ + function fourSumCount($nums1, $nums2, $nums3, $nums4) { + $map = []; + foreach ($nums1 as $n1) { + foreach ($nums2 as $n2) { + $temp = $n1 + $n2; + $map[$temp] = isset($map[$temp]) ? $map[$temp]+1 : 1; + } + } + $count = 0; + foreach ($nums3 as $n3) { + foreach ($nums4 as $n4) { + $temp = 0 - $n3 - $n4; + if (isset($map[$temp])) { + $count += $map[$temp]; + } + } + } + return $count; + } +} +``` ----------------------- From 7f30649d46cb35f2ff175f0f1309ae58fe962de9 Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:52:20 -0400 Subject: [PATCH 7/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 64503cef..32b0ff7f 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -266,6 +266,31 @@ var canConstruct = function(ransomNote, magazine) { }; ``` +PHP: +```php +class Solution { + /** + * @param String $ransomNote + * @param String $magazine + * @return Boolean + */ + function canConstruct($ransomNote, $magazine) { + if (count($ransomNote) > count($magazine)) { + return false; + } + $map = []; + for ($i = 0; $i < strlen($magazine); $i++) { + $map[$magazine[$i]] = ($map[$magazine[$i]] ?? 0) + 1; + } + for ($i = 0; $i < strlen($ransomNote); $i++) { + if (!isset($map[$ransomNote[$i]]) || --$map[$ransomNote[$i]] < 0) { + return false; + } + } + return true; + } +} +``` ----------------------- From e6975415370067128b8a625944fedec91728f4fc Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:53:36 -0400 Subject: [PATCH 8/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=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/0015.三数之和.md | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 1adb2d24..adb9a113 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -393,6 +393,46 @@ function threeSum(array $nums): array } ``` +PHP: +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer[][] + */ + function threeSum($nums) { + $res = []; + sort($nums); + for ($i = 0; $i < count($nums); $i++) { + if ($nums[$i] > 0) { + return $res; + } + if ($i > 0 && $nums[$i] == $nums[$i - 1]) { + continue; + } + $left = $i + 1; + $right = count($nums) - 1; + while ($left < $right) { + $sum = $nums[$i] + $nums[$left] + $nums[$right]; + if ($sum < 0) { + $left++; + } + else if ($sum > 0) { + $right--; + } + else { + $res[] = [$nums[$i], $nums[$left], $nums[$right]]; + while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++; + while ($left < $right && $nums[$right] == $nums[$right - 1]) $right--; + $left++; + $right--; + } + } + } + return $res; + } +} +``` ----------------------- From 3e1d341aea97d6f162606827b3e55151820fc299 Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Thu, 2 Sep 2021 23:54:21 -0400 Subject: [PATCH 9/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200018.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=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/0018.四数之和.md | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 6af033b9..c81c5df7 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -310,6 +310,49 @@ var fourSum = function(nums, target) { }; ``` +PHP: +```php +class Solution { + /** + * @param Integer[] $nums + * @param Integer $target + * @return Integer[][] + */ + function fourSum($nums, $target) { + $res = []; + sort($nums); + for ($i = 0; $i < count($nums); $i++) { + if ($i > 0 && $nums[$i] == $nums[$i - 1]) { + continue; + } + for ($j = $i + 1; $j < count($nums); $j++) { + if ($j > $i + 1 && $nums[$j] == $nums[$j - 1]) { + continue; + } + $left = $j + 1; + $right = count($nums) - 1; + while ($left < $right) { + $sum = $nums[$i] + $nums[$j] + $nums[$left] + $nums[$right]; + if ($sum < $target) { + $left++; + } + else if ($sum > $target) { + $right--; + } + else { + $res[] = [$nums[$i], $nums[$j], $nums[$left], $nums[$right]]; + while ($left < $right && $nums[$left] == $nums[$left+1]) $left++; + while ($left < $right && $nums[$right] == $nums[$right-1]) $right--; + $left++; + $right--; + } + } + } + } + return $res; + } +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)