From ceaaa2fd2d3114c75794b89fbb08005dd991b81c Mon Sep 17 00:00:00 2001 From: Epoch <75031971+messenger1th@users.noreply.github.com> Date: Mon, 21 Feb 2022 14:37:15 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E7=9A=84CPP=E6=9B=B4=E7=AE=80=E6=B4=81=E7=9A=84?= =?UTF-8?q?=E7=89=88=E6=9C=AC2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用LC27的原理使得更为简洁。 --- problems/0151.翻转字符串里的单词.md | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index ead5fa12..677a8f64 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -222,7 +222,42 @@ public: 效率: +```CPP +//版本二: +class Solution { +public: + void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。 + for(int i=start;i<(end-start)/2+start;++i){ + swap(s[i],s[end-1-i+start]); + } + } + void trim(string& s){//去除所有空格并在相邻单词之间添加空格 + int slow = 0; + for(int i=0;i Date: Mon, 14 Mar 2022 13:33:40 +0800 Subject: [PATCH 02/13] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20?= =?UTF-8?q?=E5=90=8C=E7=90=86CPP=20=E7=89=88=E6=9C=AC2=E7=AE=80=E6=B4=81?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 规范代码,优化留白。 同时添加详细注解, 并给出同理题目练习链接。 --- problems/0151.翻转字符串里的单词.md | 36 +++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 677a8f64..e7abd1d8 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -224,34 +224,36 @@ public: ```CPP //版本二: +//原理同版本1,更简洁实现。 class Solution { public: - void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。 - for(int i=start;i<(end-start)/2+start;++i){ - swap(s[i],s[end-1-i+start]); + void reverse(string& s, int start, int end){ //翻转,区间写法:闭区间 [] + for (int i = start, j = end; i < j; i++, j--) { + swap(s[i], s[j]); } } - void trim(string& s){//去除所有空格并在相邻单词之间添加空格 - int slow = 0; - for(int i=0;i Date: Sun, 20 Mar 2022 20:54:32 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node_modules/.yarn-integrity | 10 ++++++++++ problems/0077.组合优化.md | 22 ++++++++++++++++++++++ yarn.lock | 4 ++++ 3 files changed, 36 insertions(+) create mode 100644 node_modules/.yarn-integrity create mode 100644 yarn.lock diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity new file mode 100644 index 00000000..29e4357c --- /dev/null +++ b/node_modules/.yarn-integrity @@ -0,0 +1,10 @@ +{ + "systemParams": "win32-x64-83", + "modulesFolders": [], + "flags": [], + "linkedModules": [], + "topLevelPatterns": [], + "lockfileEntries": {}, + "files": [], + "artifacts": {} +} \ No newline at end of file diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 81b4304c..94608ec1 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -240,7 +240,29 @@ var combine = function(n, k) { }; ``` +TypeScript: + +```typescript +function combine(n: number, k: number): number[][] { + let resArr: number[][] = []; + function backTracking(n: number, k: number, startIndex: number, tempArr: number[]): void { + if (tempArr.length === k) { + resArr.push(tempArr.slice()); + return; + } + for (let i = startIndex; i <= n - k + 1 + tempArr.length; i++) { + tempArr.push(i); + backTracking(n, k, i + 1, tempArr); + tempArr.pop(); + } + } + backTracking(n, k, 1, []); + return resArr; +}; +``` + C: + ```c int* path; int pathTop; diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 00000000..3ff608ae --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + From 8ad12370b69a4b37f3baffe8e1102e82c394ac4a Mon Sep 17 00:00:00 2001 From: "Neil.Liu" <88214924@qq.com> Date: Sun, 20 Mar 2022 22:32:34 +0800 Subject: [PATCH 04/13] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0=E5=AE=89?= =?UTF-8?q?=E6=8E=92=E8=A1=8C=E7=A8=8B.md=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0332.重新安排行程.md | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 01f81c4d..041a7f03 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -342,6 +342,64 @@ class Solution: return path ``` +### Go +```go +type pair struct { + target string + visited bool +} +type pairs []*pair + +func (p pairs) Len() int { + return len(p) +} +func (p pairs) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} +func (p pairs) Less(i, j int) bool { + return p[i].target < p[j].target +} + +func findItinerary(tickets [][]string) []string { + result := []string{} + // map[出发机场] pair{目的地,是否被访问过} + targets := make(map[string]pairs) + for _, ticket := range tickets { + if targets[ticket[0]] == nil { + targets[ticket[0]] = make(pairs, 0) + } + targets[ticket[0]] = append(targets[ticket[0]], &pair{target: ticket[1], visited: false}) + } + for k, _ := range targets { + sort.Sort(targets[k]) + } + result = append(result, "JFK") + var backtracking func() bool + backtracking = func() bool { + if len(tickets)+1 == len(result) { + return true + } + // 取出起飞航班对应的目的地 + for _, pair := range targets[result[len(result)-1]] { + if pair.visited == false { + result = append(result, pair.target) + pair.visited = true + if backtracking() { + return true + } + result = result[:len(result)-1] + pair.visited = false + } + } + return false + } + + backtracking() + + return result +} +``` + ### C语言 ```C From 21f6068e2e3fd0c3d40971df3c0ea3b35a9ec248 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 20 Mar 2022 23:24:57 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880216.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0216.组合总和III.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 0bb42192..32b1347e 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -396,6 +396,30 @@ var combinationSum3 = function(k, n) { }; ``` +## TypeScript + +```typescript +function combinationSum3(k: number, n: number): number[][] { + const resArr: number[][] = []; + function backTracking(k: number, n: number, sum: number, startIndex: number, tempArr: number[]): void { + if (sum > n) return; + if (tempArr.length === k) { + if (sum === n) { + resArr.push(tempArr.slice()); + } + return; + } + for (let i = startIndex; i <= 9 - (k - tempArr.length) + 1; i++) { + tempArr.push(i); + backTracking(k, n, sum + i, i + 1, tempArr); + tempArr.pop(); + } + } + backTracking(k, n, 0, 1, []); + return resArr; +}; +``` + ## C ```c From 82feee15424aa110e5d30b333ea3c0a83245b3c2 Mon Sep 17 00:00:00 2001 From: dcj_hp <294487055@qq.com> Date: Mon, 21 Mar 2022 12:04:13 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20647.=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2=20=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0647.回文子串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index d9928b8f..739146f1 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -54,7 +54,7 @@ 当s[i]与s[j]相等时,这就复杂一些了,有如下三种情况 * 情况一:下标i 与 j相同,同一个字符例如a,当然是回文子串 -* 情况二:下标i 与 j相差为1,例如aa,也是文子串 +* 情况二:下标i 与 j相差为1,例如aa,也是回文子串 * 情况三:下标:i 与 j相差大于1的时候,例如cabac,此时s[i]与s[j]已经相同了,我们看i到j区间是不是回文子串就看aba是不是回文就可以了,那么aba的区间就是 i+1 与 j-1区间,这个区间是不是回文就看dp[i + 1][j - 1]是否为true。 以上三种情况分析完了,那么递归公式如下: From 74ac8c39c1248afd5d99604b7a20e27c67625679 Mon Sep 17 00:00:00 2001 From: Guang-Hou <87743934+Guang-Hou@users.noreply.github.com> Date: Fri, 25 Mar 2022 15:49:48 -0400 Subject: [PATCH 07/13] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9=E7=AB=99?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ca95af67..f73ab9f4 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -239,6 +239,30 @@ class Solution { ### Python ```python +# 解法1 +class Solution: + def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: + n = len(gas) + cur_sum = 0 + min_sum = float('inf') + + for i in range(n): + cur_sum += gas[i] - cost[i] + min_sum = min(min_sum, cur_sum) + + if cur_sum < 0: return -1 + if min_sum >= 0: return 0 + + for j in range(n - 1, 0, -1): + min_sum += gas[j] - cost[j] + if min_sum >= 0: + return j + + return -1 +``` + +```python +# 解法2 class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: start = 0 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 08/13] =?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 09/13] =?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 10/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0python=E8=A7=A3?= =?UTF-8?q?=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 11/13] =?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 From c590dfc5f29e910a85798f144bcc7178dda2bde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Thu, 7 Apr 2022 09:34:25 +0800 Subject: [PATCH 12/13] Delete yarn.lock --- yarn.lock | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 yarn.lock diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index 3ff608ae..00000000 --- a/yarn.lock +++ /dev/null @@ -1,4 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - From bbe312015eca5207064aa42f3530e254845a1e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Thu, 7 Apr 2022 09:34:51 +0800 Subject: [PATCH 13/13] Delete .yarn-integrity --- node_modules/.yarn-integrity | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 node_modules/.yarn-integrity diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity deleted file mode 100644 index 29e4357c..00000000 --- a/node_modules/.yarn-integrity +++ /dev/null @@ -1,10 +0,0 @@ -{ - "systemParams": "win32-x64-83", - "modulesFolders": [], - "flags": [], - "linkedModules": [], - "topLevelPatterns": [], - "lockfileEntries": {}, - "files": [], - "artifacts": {} -} \ No newline at end of file