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/4] =?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/4] =?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 Date: Sat, 26 Mar 2022 21:47:35 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0python=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0127.单词接龙.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 407596c0..584bcb2a 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List wordList) ``` ## Python - +``` +class Solution: + def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: + wordSet = set(wordList) + if len(wordSet)== 0 or endWord not in wordSet: + return 0 + mapping = {beginWord:1} + queue = deque([beginWord]) + while queue: + word = queue.popleft() + path = mapping[word] + for i in range(len(word)): + word_list = list(word) + for j in range(26): + word_list[i] = chr(ord('a')+j) + newWord = "".join(word_list) + if newWord == endWord: + return path+1 + if newWord in wordSet and newWord not in mapping: + mapping[newWord] = path+1 + queue.append(newWord) + return 0 +``` ## Go ## JavaScript From 0296ac0a0e4e0b7f0b63ded21004ccf24f755a60 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 28 Mar 2022 14:33:49 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesript?= =?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/0017.电话号码的字母组合.md | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 7040182f..94136565 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -420,6 +420,40 @@ var letterCombinations = function(digits) { }; ``` +## TypeScript + +```typescript +function letterCombinations(digits: string): string[] { + if (digits === '') return []; + const strMap: { [index: string]: string[] } = { + 1: [], + 2: ['a', 'b', 'c'], + 3: ['d', 'e', 'f'], + 4: ['g', 'h', 'i'], + 5: ['j', 'k', 'l'], + 6: ['m', 'n', 'o'], + 7: ['p', 'q', 'r', 's'], + 8: ['t', 'u', 'v'], + 9: ['w', 'x', 'y', 'z'], + } + const resArr: string[] = []; + function backTracking(digits: string, curIndex: number, route: string[]): void { + if (curIndex === digits.length) { + resArr.push(route.join('')); + return; + } + let tempArr: string[] = strMap[digits[curIndex]]; + for (let i = 0, length = tempArr.length; i < length; i++) { + route.push(tempArr[i]); + backTracking(digits, curIndex + 1, route); + route.pop(); + } + } + backTracking(digits, 0, []); + return resArr; +}; +``` + ## C ```c