From 9dbc51455d7f13b83fe63d30a477d89fd1e0c33a Mon Sep 17 00:00:00 2001 From: xuerbujia <83055661+xuerbujia@users.noreply.github.com> Date: Sat, 26 Mar 2022 10:03:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880141.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0141.环形链表.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md index 0712a2a2..ddd83c94 100644 --- a/problems/0141.环形链表.md +++ b/problems/0141.环形链表.md @@ -106,6 +106,21 @@ class Solution: ## Go ```go +func hasCycle(head *ListNode) bool { + if head==nil{ + return false + } //空链表一定不会有环 + fast:=head + slow:=head //快慢指针 + for fast.Next!=nil&&fast.Next.Next!=nil{ + fast=fast.Next.Next + slow=slow.Next + if fast==slow{ + return true //快慢指针相遇则有环 + } + } + return false +} ``` ### JavaScript From 1fa83c2b3f6b1eded21f2f5313be38b1a72ff2e5 Mon Sep 17 00:00:00 2001 From: xuerbujia <83055661+xuerbujia@users.noreply.github.com> Date: Sat, 26 Mar 2022 10:13:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880189.=E8=BD=AE?= =?UTF-8?q?=E8=BD=AC=E6=95=B0=E7=BB=84.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0189.旋转数组.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index bbe152a2..8e39d253 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -124,6 +124,19 @@ class Solution: ## Go ```go +func rotate(nums []int, k int) { + l:=len(nums) + index:=l-k%l + reverse(nums) + reverse(nums[:l-index]) + reverse(nums[l-index:]) +} +func reverse(nums []int){ + l:=len(nums) + for i:=0;i