From be0ef14cef18a6aeae95872c82dca0221d16f76b Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Tue, 1 Jun 2021 12:01:24 +0800 Subject: [PATCH 01/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20101.=20=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index d8797d30..b58cef2e 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -363,6 +363,54 @@ Python: Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +// 递归 +func defs(left *TreeNode, right *TreeNode) bool { + if left == nil && right == nil { + return true; + }; + if left == nil || right == nil { + return false; + }; + if left.Val != right.Val { + return false; + } + return defs(left.Left, right.Right) && defs(right.Left, left.Right); +} +func isSymmetric(root *TreeNode) bool { + return defs(root.Left, root.Right); +} + +// 迭代 +func isSymmetric(root *TreeNode) bool { + var queue []*TreeNode; + if root != nil { + queue = append(queue, root.Left, root.Right); + } + for len(queue) > 0 { + left := queue[0]; + right := queue[1]; + queue = queue[2:]; + if left == nil && right == nil { + continue; + } + if left == nil || right == nil || left.Val != right.Val { + return false; + }; + queue = append(queue, left.Left, right.Right, right.Left, left.Right); + } + return true; +} +``` + JavaScript ```javascript From 0ddb3430986065f6a248c7936c3351544615161d Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Tue, 1 Jun 2021 12:22:46 +0800 Subject: [PATCH 02/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 756afb68..5f0fe411 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -284,6 +284,55 @@ Python: Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func max (a, b int) int { + if a > b { + return a; + } + return b; +} +// 递归 +func maxDepth(root *TreeNode) int { + if root == nil { + return 0; + } + return max(maxDepth(root.Left), maxDepth(root.Right)) + 1; +} + +// 遍历 +func maxDepth(root *TreeNode) int { + levl := 0; + queue := make([]*TreeNode, 0); + if root != nil { + queue = append(queue, root); + } + for l := len(queue); l > 0; { + for ;l > 0;l-- { + node := queue[0]; + if node.Left != nil { + queue = append(queue, node.Left); + } + if node.Right != nil { + queue = append(queue, node.Right); + } + queue = queue[1:]; + } + levl++; + l = len(queue); + } + return levl; +} + +``` + JavaScript ```javascript From 51edb80d7c8771b0b9af932a577daeb658efd100 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Tue, 1 Jun 2021 14:20:54 +0800 Subject: [PATCH 03/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 8ee15eac..48795722 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -301,6 +301,64 @@ class Solution: Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func min(a, b int) int { + if a < b { + return a; + } + return b; +} +// 递归 +func minDepth(root *TreeNode) int { + if root == nil { + return 0; + } + if root.Left == nil && root.Right != nil { + return 1 + minDepth(root.Right); + } + if root.Right == nil && root.Left != nil { + return 1 + minDepth(root.Left); + } + return min(minDepth(root.Left), minDepth(root.Right)) + 1; +} + +// 迭代 + +func minDepth(root *TreeNode) int { + dep := 0; + queue := make([]*TreeNode, 0); + if root != nil { + queue = append(queue, root); + } + for l := len(queue); l > 0; { + dep++; + for ; l > 0; l-- { + node := queue[0]; + if node.Left == nil && node.Right == nil { + return dep; + } + if node.Left != nil { + queue = append(queue, node.Left); + } + if node.Right != nil { + queue = append(queue, node.Right); + } + queue = queue[1:]; + } + l = len(queue); + } + return dep; +} +``` + JavaScript: From ea88604bfdf249d686e1aba4a26795a35a57dcc8 Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 1 Jun 2021 02:44:13 -0700 Subject: [PATCH 04/28] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index d9dfc62c..51a903ef 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -184,6 +184,55 @@ class Solution { Python: +这里使用了前缀表统一减一的实现方式 + +```python +class Solution: + def repeatedSubstringPattern(self, s: str) -> bool: + if len(s) == 0: + return False + nxt = [0] * len(s) + self.getNext(nxt, s) + if nxt[-1] != -1 and len(s) % (len(s) - (nxt[-1] + 1)) == 0: + return True + return False + + def getNext(self, nxt, s): + nxt[0] = -1 + j = -1 + for i in range(1, len(s)): + while j >= 0 and s[i] != s[j+1]: + j = nxt[j] + if s[i] == s[j+1]: + j += 1 + nxt[i] = j + return nxt +``` + +前缀表(不减一)的代码实现 + +```python +class Solution: + def repeatedSubstringPattern(self, s: str) -> bool: + if len(s) == 0: + return False + nxt = [0] * len(s) + self.getNext(nxt, s) + if nxt[-1] != 0 and len(s) % (len(s) - nxt[-1]) == 0: + return True + return False + + def getNext(self, nxt, s): + nxt[0] = 0 + j = 0 + for i in range(1, len(s)): + while j > 0 and s[i] != s[j]: + j = nxt[j - 1] + if s[i] == s[j]: + j += 1 + nxt[i] = j + return nxt +``` Go: From 3362183d8446e551f0361ead95dca6bec8b7411a Mon Sep 17 00:00:00 2001 From: boom-jumper <56831966+boom-jumper@users.noreply.github.com> Date: Tue, 1 Jun 2021 18:08:16 +0800 Subject: [PATCH 05/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A00347=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0=20python3=20=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/0347.前K个高频元素.md | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 841584b4..29963b48 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -162,6 +162,63 @@ class Solution { Python: +```python +class Solution: + def sift(self, alist, low, high): + '''小根堆构建''' + i = low + j = 2 * i + 1 + tmp = alist[low] + while j <= high: + if j + 1 <= high and alist[j+1] <= alist[j]: + j += 1 + if alist[j] < tmp: + alist[i] = alist[j] + i = j + j = 2 * i + 1 + else: + alist[i] = tmp + break + else: + alist[i] = tmp + + + def topK(self, nums, k): + # 建立小根堆 + heap = nums[:k] + for i in range((k-2)//2, -1, -1): + self.sift(heap, i, k-1) + + # 把后续的k到len(nums)填充到小根堆里 + for i in range(k, len(nums)): + if nums[i] >= heap[0]: + heap[0] = nums[i] + self.sift(heap, 0, k-1) + + # 排序 + for i in range(k-1, -1, -1): + heap[0], heap[i]= heap[i], heap[0] + self.sift(heap, 0, i-1) + return heap + + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + dict1 = dict() + for val in nums: + if val not in dict1: + dict1[val] = 1 + else: + dict1[val] += 1 + res = list() + ind = list() + for item in dict1: + res.append([dict1[item], item]) + result = list() + heap = self.topK(res, k) + print(heap) + for val in heap: + result.append(val[1]) + return result +``` Go: From d9e4b337243d51e39bf378737c8c2eac600de3d8 Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Tue, 1 Jun 2021 03:47:40 -0700 Subject: [PATCH 06/28] =?UTF-8?q?Update=200239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index ed17157a..47383a47 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -263,6 +263,41 @@ class Solution { ``` Python: +```python +class MyQueue: #单调队列(从大到小 + def __init__(self): + self.queue = [] #使用list来实现单调队列 + + #每次弹出的时候,比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。 + #同时pop之前判断队列当前是否为空。 + def pop(self, value): + if self.queue and value == self.queue[0]: + self.queue.pop(0)#list.pop()时间复杂度为O(n),这里可以使用collections.deque() + + #如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。 + #这样就保持了队列里的数值是单调从大到小的了。 + def push(self, value): + while self.queue and value > self.queue[-1]: + self.queue.pop() + self.queue.append(value) + + #查询当前队列里的最大值 直接返回队列前端也就是front就可以了。 + def front(self): + return self.queue[0] + +class Solution: + def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: + que = MyQueue() + result = [] + for i in range(k): #先将前k的元素放进队列 + que.push(nums[i]) + result.append(que.front()) #result 记录前k的元素的最大值 + for i in range(k, len(nums)): + que.pop(nums[i - k]) #滑动窗口移除最前面元素 + que.push(nums[i]) #滑动窗口前加入最后面的元素 + result.append(que.front()) #记录对应的最大值 + return result +``` Go: From 9219242b3af749a96efbbaf7d8ea5089a36ba7fe Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 1 Jun 2021 20:34:57 +0800 Subject: [PATCH 07/28] =?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=20go=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用map方式解题,降低时间复杂度 --- problems/0001.两数之和.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 77318294..3d0674ce 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -134,6 +134,20 @@ func twoSum(nums []int, target int) []int { } return []int{} } + +```go +// 使用map方式解题,降低时间复杂度 +func twoSum(nums []int, target int) []int { + m := make(map[int]int) + for index, val := range nums { + if preIndex, ok := m[target-val]; ok { + return []int{preIndex, index} + } else { + m[val] = index + } + } + return []int{} +} ``` Rust From 36626386c329d75524843fab42f740e548578173 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 1 Jun 2021 20:35:55 +0800 Subject: [PATCH 08/28] =?UTF-8?q?=E6=9B=B4=E6=94=B9=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 3d0674ce..4a1a987f 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -134,6 +134,7 @@ func twoSum(nums []int, target int) []int { } return []int{} } +``` ```go // 使用map方式解题,降低时间复杂度 From ce970df5ffdb2e428b4ec2417e2bbccdc9371a31 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 1 Jun 2021 23:43:02 +0800 Subject: [PATCH 09/28] =?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=20go=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 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 2c648f58..5291060c 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -154,6 +154,23 @@ class Solution(object): Go: +```go +func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { + m := make(map[int]int) + count := 0 + for _, v1 := range nums1 { + for _, v2 := range nums2 { + m[v1+v2]++ + } + } + for _, v3 := range nums3 { + for _, v4 := range nums4 { + count += m[-v3-v4] + } + } + return count +} +``` javaScript: From 67313906d16f8cf0e613968bf0f9b8031a273cdd Mon Sep 17 00:00:00 2001 From: Yang Date: Tue, 1 Jun 2021 14:41:22 -0400 Subject: [PATCH 10/28] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update Java solution. For sorting, no need to compare the second element in an interval. --- problems/0056.合并区间.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index eb8e7bff..f42fced7 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -144,11 +144,7 @@ class Solution { Arrays.sort(intervals, new Comparator() { @Override public int compare(int[] o1, int[] o2) { - if (o1[0] != o2[0]) { - return Integer.compare(o1[0],o2[0]); - } else { - return Integer.compare(o1[1],o2[1]); - } + return Integer.compare(o1[0], o2[0]); } }); From 838e290312af9218c62630a18022087205c8a2bc Mon Sep 17 00:00:00 2001 From: Yang Date: Tue, 1 Jun 2021 14:46:19 -0400 Subject: [PATCH 11/28] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify sorting --- problems/0056.合并区间.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index f42fced7..43ff447d 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -141,12 +141,7 @@ Java: class Solution { public int[][] merge(int[][] intervals) { List res = new LinkedList<>(); - Arrays.sort(intervals, new Comparator() { - @Override - public int compare(int[] o1, int[] o2) { - return Integer.compare(o1[0], o2[0]); - } - }); + Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]); int start = intervals[0][0]; for (int i = 1; i < intervals.length; i++) { From f4736c7dd2fb5ce75864dc34baf5e4bf212c16d4 Mon Sep 17 00:00:00 2001 From: Yang Date: Tue, 1 Jun 2021 14:48:52 -0400 Subject: [PATCH 12/28] Improve comparison --- problems/0056.合并区间.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 43ff447d..d4ffc554 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -141,7 +141,7 @@ Java: class Solution { public int[][] merge(int[][] intervals) { List res = new LinkedList<>(); - Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]); + Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0])); int start = intervals[0][0]; for (int i = 1; i < intervals.length; i++) { From bb5cfa68b2a20bc3609b56cd65d2542630c86f37 Mon Sep 17 00:00:00 2001 From: "Neil.Liu" <88214924@qq.com> Date: Wed, 2 Jun 2021 10:15:47 +0800 Subject: [PATCH 13/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A00151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 56 +++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 512360fe..63499b71 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -318,9 +318,61 @@ class Solution { Python: - Go: +```go +import ( + "fmt" +) + +func reverseWords(s string) string { + //1.使用双指针删除冗余的空格 + slowIndex, fastIndex := 0, 0 + b := []byte(s) + //删除头部冗余空格 + for len(b) > 0 && fastIndex < len(b) && b[fastIndex] == ' ' { + fastIndex++ + } + //删除单词间冗余空格 + for ; fastIndex < len(b); fastIndex++ { + if fastIndex-1 > 0 && b[fastIndex-1] == b[fastIndex] && b[fastIndex] == ' ' { + continue + } + b[slowIndex] = b[fastIndex] + slowIndex++ + } + //删除尾部冗余空格 + if slowIndex-1 > 0 && b[slowIndex-1] == ' ' { + b = b[:slowIndex-1] + } else { + b = b[:slowIndex] + } + //2.反转整个字符串 + reverse(&b, 0, len(b)-1) + //3.反转单个单词 i单词开始位置,j单词结束位置 + i := 0 + for i < len(b) { + j := i + for ; j < len(b) && b[j] != ' '; j++ { + } + reverse(&b, i, j-1) + i = j + i++ + } + return string(b) +} + +func reverse(b *[]byte, left, right int) { + for left < right { + (*b)[left], (*b)[right] = (*b)[right], (*b)[left] + left++ + right-- + } +} +``` + + + @@ -328,4 +380,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
\ No newline at end of file From fae5373477b50d259c94c08dcef1ce6254081491 Mon Sep 17 00:00:00 2001 From: "Neil.Liu" <88214924@qq.com> Date: Wed, 2 Jun 2021 10:25:09 +0800 Subject: [PATCH 14/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=89=91=E6=8C=87Offer?= =?UTF-8?q?58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../剑指Offer58-II.左旋转字符串.md | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 648546c1..39c8382c 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -23,7 +23,7 @@ https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/ 示例 2: 输入: s = "lrloseumgh", k = 6 输出: "umghlrlose" -  + 限制: 1 <= k < s.length <= 10000 @@ -119,9 +119,31 @@ class Solution { ``` Python: - Go: +```go +func reverseLeftWords(s string, n int) string { + b := []byte(s) + // 1. 反转前n个字符 + // 2. 反转第n到end字符 + // 3. 反转整个字符 + reverse(b, 0, n-1) + reverse(b, n, len(b)-1) + reverse(b, 0, len(b)-1) + return string(b) +} +// 切片是引用传递 +func reverse(b []byte, left, right int){ + for left < right{ + b[left], b[right] = b[right],b[left] + left++ + right-- + } +} +``` + + + @@ -129,4 +151,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
\ No newline at end of file From 9163ed6b65667e4c95a9ed2b26bd63a6a516fc9f Mon Sep 17 00:00:00 2001 From: zhangzw Date: Wed, 2 Jun 2021 11:49:59 +0800 Subject: [PATCH 15/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A01049.=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F?= =?UTF-8?q?II=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1049.最后一块石头的重量II.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index f74600b1..c09e476a 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -191,7 +191,33 @@ class Solution: ``` Go: +```go +func lastStoneWeightII(stones []int) int { + // 15001 = 30 * 1000 /2 +1 + dp := make([]int, 15001) + // 求target + sum := 0 + for _, v := range stones { + sum += v + } + target := sum / 2 + // 遍历顺序 + for i := 0; i < len(stones); i++ { + for j := target; j >= stones[i]; j-- { + // 推导公式 + dp[j] = max(dp[j], dp[j-stones[i]]+stones[i]) + } + } + return sum - 2 * dp[target] +} +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` From 9f84671e690208a1b40debe15b1bfa624448f348 Mon Sep 17 00:00:00 2001 From: zhangzw Date: Wed, 2 Jun 2021 11:51:50 +0800 Subject: [PATCH 16/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A00494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index c772a09d..c917ed5c 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -276,7 +276,35 @@ class Solution: ``` Go: - +```go +func findTargetSumWays(nums []int, target int) int { + sum := 0 + for _, v := range nums { + sum += v + } + if target > sum { + return 0 + } + if (sum+target)%2 == 1 { + return 0 + } + // 计算背包大小 + bag := (sum + target) / 2 + // 定义dp数组 + dp := make([]int, bag+1) + // 初始化 + dp[0] = 1 + // 遍历顺序 + for i := 0; i < len(nums); i++ { + for j := bag; j >= nums[i]; j-- { + //推导公式 + dp[j] += dp[j-nums[i]] + //fmt.Println(dp) + } + } + return dp[bag] +} +``` From 9707fbc521b16e79a43ef588e4f4a77454a30fdf Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Wed, 2 Jun 2021 02:41:59 -0700 Subject: [PATCH 17/28] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 841584b4..71af618e 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -162,7 +162,33 @@ class Solution { Python: - +```python +#时间复杂度:O(nlogk) +#空间复杂度:O(n) +import heapq +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + #要统计元素出现频率 + map_ = {} #nums[i]:对应出现的次数 + for i in range(len(nums)): + map_[nums[i]] = map_.get(nums[i], 0) + 1 + + #对频率排序 + #定义一个小顶堆,大小为k + pri_que = [] #小顶堆 + + #用固定大小为k的小顶堆,扫面所有频率的数值 + for key, freq in map_.items(): + heapq.heappush(pri_que, (freq, key)) + if len(pri_que) > k: #如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k + heapq.heappop(pri_que) + + #找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组 + result = [0] * k + for i in range(k-1, -1, -1): + result[i] = heapq.heappop(pri_que)[1] + return result +``` Go: From 2a7c3cb87c2fbcefe1b1995cfa0d45ba7e857627 Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Wed, 2 Jun 2021 03:09:05 -0700 Subject: [PATCH 18/28] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 71 +++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index dca5d3e3..dc79643d 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -239,7 +239,78 @@ Java: ``` Python: +> 迭代法前序遍历 +```python +class Solution: + def preorderTraversal(self, root: TreeNode) -> List[int]: + result = [] + st= [] + if root: + st.append(root) + while st: + node = st.pop() + if node != None: + if node.right: #右 + st.append(node.right) + if node.left: #左 + st.append(node.left) + st.append(node) #中 + st.append(None) + else: + node = st.pop() + result.append(node.val) + return result +``` + +> 迭代法中序遍历 +```python +class Solution: + def inorderTraversal(self, root: TreeNode) -> List[int]: + result = [] + st = [] + if root: + st.append(root) + while st: + node = st.pop() + if node != None: + if node.right: #添加右节点(空节点不入栈) + st.append(node.right) + + st.append(node) #添加中节点 + st.append(None) #中节点访问过,但是还没有处理,加入空节点做为标记。 + + if node.left: #添加左节点(空节点不入栈) + st.append(node.left) + else: #只有遇到空节点的时候,才将下一个节点放进结果集 + node = st.pop() #重新取出栈中元素 + result.append(node.val) #加入到结果集 + return result +``` + +> 迭代法后序遍历 +```python +class Solution: + def postorderTraversal(self, root: TreeNode) -> List[int]: + result = [] + st = [] + if root: + st.append(root) + while st: + node = st.pop() + if node != None: + st.append(node) #中 + st.append(None) + + if node.right: #右 + st.append(node.right) + if node.left: #左 + st.append(node.left) + else: + node = st.pop() + result.append(node.val) + return result +``` Go: > 前序遍历统一迭代法 From 045f3f98d6234e548c8b98edee911f2eb39b482f Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Wed, 2 Jun 2021 20:10:28 +0800 Subject: [PATCH 19/28] =?UTF-8?q?0134.=E5=8A=A0=E6=B2=B9=E7=AB=99.md=20Jav?= =?UTF-8?q?ascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 5c3f70a8..dfed2d96 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -241,7 +241,28 @@ class Solution: Go: +Javascript: +```Javascript +var canCompleteCircuit = function(gas, cost) { + const gasLen = gas.length + let start = 0 + let curSum = 0 + let totalSum = 0 + for(let i = 0; i < gasLen; i++) { + curSum += gas[i] - cost[i] + totalSum += gas[i] - cost[i] + if(curSum < 0) { + curSum = 0 + start = i + 1 + } + } + + if(totalSum < 0) return -1 + + return start +}; +``` ----------------------- From bed3f0b09fc46f11d319f33225e78e7c12d5842b Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Wed, 2 Jun 2021 20:11:11 +0800 Subject: [PATCH 20/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=20go=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index fd8175db..23e2c5fd 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -166,6 +166,21 @@ class Solution(object): ``` Go: +```go +func canConstruct(ransomNote string, magazine string) bool { + record := make([]int, 26) + for _, v := range magazine { + record[v-'a']++ + } + for _, v := range ransomNote { + record[v-'a']-- + if record[v-'a'] < 0 { + return false + } + } + return true +} +``` javaScript: From 41c4f17833df822a740ae2b901fb3bb530b5b47b Mon Sep 17 00:00:00 2001 From: evanlai Date: Wed, 2 Jun 2021 23:39:59 +0800 Subject: [PATCH 21/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index b3714c43..ecf7f132 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -134,7 +134,21 @@ class Solution { ``` Python: - +```python +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + #先考虑胃口小的孩子 + g.sort() + s.sort() + i=j=0 + count = 0 + while(i Date: Wed, 2 Jun 2021 23:41:29 +0800 Subject: [PATCH 22/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 6fa30cde..bbca5ea0 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -138,7 +138,21 @@ class Solution { ``` Python: - +```python +class Solution: + def wiggleMaxLength(self, nums: List[int]) -> int: + #贪心 求波峰数量 + 波谷数量 + if len(nums)<=1: + return len(nums) + cur, pre = 0,0 #当前一对差值,前一对差值 + count = 1#默认最右边有一个峰值 + for i in range(len(nums)-1): + cur = nums[i+1] - nums[i] + if((cur>0 and pre<=0) or (cur<0 and pre>=0)): + count += 1 + pre = cur + return count +``` Go: From 37515f2202b8a9868a25f8c000f46fe589a093a3 Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Thu, 3 Jun 2021 08:25:39 +0800 Subject: [PATCH 23/28] =?UTF-8?q?0135.=E5=88=86=E5=8F=91=E7=B3=96=E6=9E=9C?= =?UTF-8?q?.md=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0135.分发糖果.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index a3311791..fd791277 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -176,7 +176,30 @@ class Solution: Go: +Javascript: +```Javascript +var candy = function(ratings) { + let candys = new Array(ratings.length).fill(1) + for(let i = 1; i < ratings.length; i++) { + if(ratings[i] > ratings[i - 1]) { + candys[i] = candys[i - 1] + 1 + } + } + + for(let i = ratings.length - 2; i >= 0; i--) { + if(ratings[i] > ratings[i + 1]) { + candys[i] = Math.max(candys[i], candys[i + 1] + 1) + } + } + + let count = candys.reduce((a, b) => { + return a + b + }) + + return count +}; +``` ----------------------- From 1a4a287243e43649166fea0d028dc8b733602387 Mon Sep 17 00:00:00 2001 From: nmydt <62681228+nmydt@users.noreply.github.com> Date: Thu, 3 Jun 2021 13:00:14 +0800 Subject: [PATCH 24/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C=20JAVA=20=E8=BF=AD=E4=BB=A3=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/0112.路径总和.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 4ccd8912..d810a046 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -345,6 +345,36 @@ class Solution { return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } } +``` +迭代 +```java +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { + if(root==null)return false; + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); + stack1.push(root);stack2.push(root.val); + while(!stack1.isEmpty()){ + int size = stack1.size(); + for(int i=0;i Date: Thu, 3 Jun 2021 14:30:31 +0800 Subject: [PATCH 25/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A00028.=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?strStr=20Go=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0028.实现strStr.md | 90 ++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index b8ebcaa1..1dba5a38 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -726,10 +726,98 @@ class Solution: Go: +```go +// 方法一:前缀表使用减1实现 + +// getNext 构造前缀表next +// params: +// next 前缀表数组 +// s 模式串 +func getNext(next []int, s string) { + j := -1 // j表示 最长相等前后缀长度 + next[0] = j + + for i := 1; i < len(s); i++ { + for j >= 0 && s[i] != s[j+1] { + j = next[j] // 回退前一位 + } + if s[i] == s[j+1] { + j++ + } + next[i] = j // next[i]是i(包括i)之前的最长相等前后缀长度 + } +} +func strStr(haystack string, needle string) int { + if len(needle) == 0 { + return 0 + } + next := make([]int, len(needle)) + getNext(next, needle) + j := -1 // 模式串的起始位置 next为-1 因此也为-1 + for i := 0; i < len(haystack); i++ { + for j >= 0 && haystack[i] != needle[j+1] { + j = next[j] // 寻找下一个匹配点 + } + if haystack[i] == needle[j+1] { + j++ + } + if j == len(needle)-1 { // j指向了模式串的末尾 + return i - len(needle) + 1 + } + } + return -1 +} +``` + +```go +// 方法二: 前缀表无减一或者右移 + +// getNext 构造前缀表next +// params: +// next 前缀表数组 +// s 模式串 +func getNext(next []int, s string) { + j := 0 + next[0] = j + for i := 1; i < len(s); i++ { + for j > 0 && s[i] != s[j] { + j = next[j-1] + } + if s[i] == s[j] { + j++ + } + next[i] = j + } +} +func strStr(haystack string, needle string) int { + n := len(needle) + if n == 0 { + return 0 + } + j := 0 + next := make([]int, n) + getNext(next, needle) + for i := 0; i < len(haystack); i++ { + for j > 0 && haystack[i] != needle[j] { + j = next[j-1] // 回退到j的前一位 + } + if haystack[i] == needle[j] { + j++ + } + if j == n { + return i - n + 1 + } + } + return -1 +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
\ No newline at end of file From 7bf42a83f42131f87c881a42567994379ceb6b8c Mon Sep 17 00:00:00 2001 From: "Neil.Liu" <88214924@qq.com> Date: Thu, 3 Jun 2021 15:42:44 +0800 Subject: [PATCH 26/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A00459.=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=20Go=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 60 +++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 51a903ef..deb755bf 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -236,6 +236,64 @@ class Solution: Go: +这里使用了前缀表统一减一的实现方式 + +```go +func repeatedSubstringPattern(s string) bool { + n := len(s) + if n == 0 { + return false + } + next := make([]int, n) + j := -1 + next[0] = j + for i := 1; i < n; i++ { + for j >= 0 && s[i] != s[j+1] { + j = next[j] + } + if s[i] == s[j+1] { + j++ + } + next[i] = j + } + // next[n-1]+1 最长相同前后缀的长度 + if next[n-1] != -1 && n%(n-(next[n-1]+1)) == 0 { + return true + } + return false +} +``` + +前缀表(不减一)的代码实现 + +```go +func repeatedSubstringPattern(s string) bool { + n := len(s) + if n == 0 { + return false + } + j := 0 + next := make([]int, n) + next[0] = j + for i := 1; i < n; i++ { + for j > 0 && s[i] != s[j] { + j = next[j-1] + } + if s[i] == s[j] { + j++ + } + next[i] = j + } + // next[n-1] 最长相同前后缀的长度 + if next[n-1] != 0 && n%(n-next[n-1]) == 0 { + return true + } + return false +} +``` + + + @@ -243,4 +301,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
\ No newline at end of file From 36957838cb9b449ba118d5762e401933258acdf8 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Thu, 3 Jun 2021 10:41:52 +0200 Subject: [PATCH 27/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200037.=E8=A7=A3?= =?UTF-8?q?=E6=95=B0=E7=8B=AC=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0037.解数独 python3版本 --- problems/0037.解数独.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index d5df95e8..4eb60704 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -287,7 +287,39 @@ class Solution { ``` Python: - +```python3 +class Solution: + def solveSudoku(self, board: List[List[str]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + def backtrack(board): + for i in range(len(board)): #遍历行 + for j in range(len(board[0])): #遍历列 + if board[i][j] != ".": continue + for k in range(1,10): #(i, j) 这个位置放k是否合适 + if isValid(i,j,k,board): + board[i][j] = str(k) #放置k + if backtrack(board): return True #如果找到合适一组立刻返回 + board[i][j] = "." #回溯,撤销k + return False #9个数都试完了,都不行,那么就返回false + return True #遍历完没有返回false,说明找到了合适棋盘位置了 + def isValid(row,col,val,board): + for i in range(9): #判断行里是否重复 + if board[row][i] == str(val): + return False + for j in range(9): #判断列里是否重复 + if board[j][col] == str(val): + return False + startRow = (row // 3) * 3 + startcol = (col // 3) * 3 + for i in range(startRow,startRow + 3): #判断9方格里是否重复 + for j in range(startcol,startcol + 3): + if board[i][j] == str(val): + return False + return True + backtrack(board) +``` Go: From 0dd4590945249059a37638bfe3ab79dc134b8e40 Mon Sep 17 00:00:00 2001 From: Yang Li Date: Thu, 3 Jun 2021 18:44:16 -0400 Subject: [PATCH 28/28] =?UTF-8?q?Update=20=E9=93=BE=E8=A1=A8=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix typo --- problems/链表理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index ea3b9098..252247c7 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -83,7 +83,7 @@ struct ListNode { 有同学说了,我不定义构造函数行不行,答案是可以的,C++默认生成一个构造函数。 -但是这个构造函数不会初始化任何成员变化,下面我来举两个例子: +但是这个构造函数不会初始化任何成员变量,下面我来举两个例子: 通过自己定义构造函数初始化节点: