From 8fbfc3af8c65aa7e2061bb89c028dc77f0c05f3a Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Wed, 12 May 2021 21:01:01 +0800 Subject: [PATCH 01/22] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index afc1f144..ec1335c6 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -208,8 +208,21 @@ Java: Python: -Go: +```Go +func invertTree(root *TreeNode) *TreeNode { + if root ==nil{ + return nil + } + temp:=root.Left + root.Left=root.Right + root.Right=temp + + invertTree(root.Left) + invertTree(root.Right) + return root +} +``` From 2132ce0e1634f65c585df3b112d8dca3a67ce0d3 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 09:03:10 +0800 Subject: [PATCH 02/22] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN=E4=B8=AA?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 3b6dde1e..3b89dabd 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -112,7 +112,28 @@ class Solution { } } ``` +Go: +```Go +func removeNthFromEnd(head *ListNode, n int) *ListNode { + result:=&ListNode{} + result.Next=head + var pre *ListNode + cur:=result + i:=1 + for head!=nil{ + if i>=n{ + pre=cur + cur=cur.Next + } + head=head.Next + i++ + } + pre.Next=pre.Next.Next + return result.Next + +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From cf42d80efc34e549f73c91dd22f2fbdbcbd39778 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 09:24:19 +0800 Subject: [PATCH 03/22] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 55e22887..96dc1ac3 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -221,6 +221,40 @@ Python: Go: +```Go +func threeSum(nums []int)[][]int{ + sort.Ints(nums) + res:=[][]int{} + + for i:=0;i0{ + break + } + if i>0&&n1==nums[i-1]{ + continue + } + l,r:=i+1,len(nums)-1 + for l Date: Thu, 13 May 2021 09:32:54 +0800 Subject: [PATCH 04/22] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0198.打家劫舍.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index b9ccec45..c64648ad 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -117,6 +117,33 @@ Python: Go: +```Go +func rob(nums []int) int { + if len(nums)<1{ + return 0 + } + if len(nums)==1{ + return nums[0] + } + if len(nums)==2{ + return max(nums[0],nums[1]) + } + dp :=make([]int,len(nums)) + dp[0]=nums[0] + dp[1]=max(nums[0],nums[1]) + for i:=2;ib{ + return a + } + return b +} +``` From 10f35bd6269c6a3b9096972a93bfe63d7e5e4094 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 09:39:02 +0800 Subject: [PATCH 05/22] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index b8a9d748..e6d3f1c8 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -145,8 +145,29 @@ Python: Go: +```Go +func maxSubArray(nums []int) int { + if len(nums)<1{ + return 0 + } + dp:=make([]int,len(nums)) + result:=nums[0] + dp[0]=nums[0] + for i:=1;ib{ + return a + }else{ + return b + } +} +``` ----------------------- From a14aafbaec8b0ad94d1b9fe7e994491c7f4e747b Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 09:43:05 +0800 Subject: [PATCH 06/22] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 6ae6adc7..f0a08f99 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -230,7 +230,20 @@ class Solution: ``` Go: - +```Go +func climbStairs(n int) int { + if n==1{ + return 1 + } + dp:=make([]int,n+1) + dp[1]=1 + dp[2]=2 + for i:=3;i<=n;i++{ + dp[i]=dp[i-1]+dp[i-2] + } + return dp[n] +} +``` From f854cbe7ae7e5daf796a9b3c27c05dd555327cab Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 10:07:16 +0800 Subject: [PATCH 07/22] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index ed0cb9d7..6e38b4fe 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -257,6 +257,44 @@ Python: Go: +```Go +func deleteNode(root *TreeNode, key int) *TreeNode { + if root==nil{ + return nil + } + if keyroot.Val{ + root.Right=deleteNode(root.Right,key) + return root + } + if root.Right==nil{ + return root.Left + } + if root.Left==nil{ + return root.Right + } + minnode:=root.Right + for minnode.Left!=nil{ + minnode=minnode.Left + } + root.Val=minnode.Val + root.Right=deleteNode1(root.Right) + return root +} + +func deleteNode1(root *TreeNode)*TreeNode{ + if root.Left==nil{ + pRight:=root.Right + root.Right=nil + return pRight + } + root.Left=deleteNode1(root.Left) + return root +} +``` From 770328ecd0673c2f5ba887f3282a75d8aebd3ab5 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 10:10:53 +0800 Subject: [PATCH 08/22] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 5d55910c..919ec7c7 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -361,7 +361,40 @@ Python: Go: - +```Go +func isBalanced(root *TreeNode) bool { + if root==nil{ + return true + } + if !isBalanced(root.Left) || !isBalanced(root.Right){ + return false + } + LeftH:=maxdepth(root.Left)+1 + RightH:=maxdepth(root.Right)+1 + if abs(LeftH-RightH)>1{ + return false + } + return true +} +func maxdepth(root *TreeNode)int{ + if root==nil{ + return 0 + } + return max(maxdepth(root.Left),maxdepth(root.Right))+1 +} +func max(a,b int)int{ + if a>b{ + return a + } + return b +} +func abs(a int)int{ + if a<0{ + return -a + } + return a +} +``` From f56d49a94f5d75abbe2f645212a77efd8b3bfa25 Mon Sep 17 00:00:00 2001 From: zqh1059405318 <1059405318@qq.com> Date: Thu, 13 May 2021 10:16:02 +0800 Subject: [PATCH 09/22] =?UTF-8?q?=E6=8F=90=E4=BA=A4242=E9=A2=98java?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index be553c5a..6c5a90ce 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -85,7 +85,24 @@ public: Java: +```java +public boolean isAnagram(String s, String t) { + int[] record = new int[26]; + for (char c : s.toCharArray()) { + record[c - 'a'] += 1; + } + for (char c : t.toCharArray()) { + record[c - 'a'] -= 1; + } + for (int i : record) { + if (i != 0) { + return false; + } + } + return true; +} +``` Python: From 645c93b688f33a69fb6220e35bd52959233e785f Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 10:19:25 +0800 Subject: [PATCH 10/22] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index baa3f435..ddd634b4 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -260,7 +260,25 @@ Python: Go: +```Go +import "math" +func isValidBST(root *TreeNode) bool { + if root == nil { + return true + } + return isBST(root, math.MinInt64, math.MaxFloat64) +} +func isBST(root *TreeNode, min, max int) bool { + if root == nil { + return true + } + if min >= root.Val || max <= root.Val { + return false + } + return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max) +} +``` From aeec6d756c01b8765b40fd9e106c7c9efdaa9fc5 Mon Sep 17 00:00:00 2001 From: simonhancrew <597494370@qq.com> Date: Thu, 13 May 2021 10:34:16 +0800 Subject: [PATCH 11/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=20Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 144cd5be..959474fc 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -124,9 +124,19 @@ public: Java: - Python: +```python +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + i,n = 0,len(nums) + for j in range(n): + if nums[j] != val: + nums[i] = nums[j] + i += 1 + return i +``` + Go: ```go @@ -162,4 +172,4 @@ var removeElement = (nums, val) => { * 作者微信:[程序员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 1613d2b4b0627c2b99c77c54590d77a1cb1ab3fc Mon Sep 17 00:00:00 2001 From: zqh1059405318 <1059405318@qq.com> Date: Thu, 13 May 2021 11:31:02 +0800 Subject: [PATCH 12/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E7=AC=AC202?= =?UTF-8?q?=EF=BC=8C242=EF=BC=8C349=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 21 ++++++++++++++++++ problems/0242.有效的字母异位词.md | 27 ++++++++++++----------- problems/0349.两个数组的交集.md | 24 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index d6b1cdd2..8c0dd1e7 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -84,7 +84,28 @@ public: Java: +```java +class Solution { + public boolean isHappy(int n) { + Set record = new HashSet<>(); + while (n != 1 && !record.contains(n)) { + record.add(n); + n = getNextNumber(n); + } + return n == 1; + } + private int getNextNumber(int n) { + int res = 0; + while (n > 0) { + int temp = n % 10; + res += temp * temp; + n = n / 10; + } + return res; + } +} +``` Python: diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 6c5a90ce..89186620 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -86,22 +86,23 @@ public: Java: ```java -public boolean isAnagram(String s, String t) { - int[] record = new int[26]; - for (char c : s.toCharArray()) { - record[c - 'a'] += 1; - } - for (char c : t.toCharArray()) { - record[c - 'a'] -= 1; - } - for (int i : record) { - if (i != 0) { - return false; +class Solution { + public boolean isAnagram(String s, String t) { + int[] record = new int[26]; + for (char c : s.toCharArray()) { + record[c - 'a'] += 1; } + for (char c : t.toCharArray()) { + record[c - 'a'] -= 1; + } + for (int i : record) { + if (i != 0) { + return false; + } + } + return true; } - return true; } - ``` Python: diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index c196b467..195dbad5 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -75,7 +75,31 @@ public: Java: +```java +class Solution { + public int[] intersection(int[] nums1, int[] nums2) { + Set record = new HashSet(); + Set unique = new HashSet(); + for (int num : nums1) { + record.add(num); + } + + for (int num : nums2) { + if (record.contains(num)) { + unique.add(num); + } + } + + int[] res = new int[unique.size()]; + int index = 0; + for (int num : unique) { + res[index++] = num; + } + return res; + } +} +``` Python: From 74e6290385025ea76c71687fcf343aa2c6cab624 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 12:53:02 +0800 Subject: [PATCH 13/22] =?UTF-8?q?Update=200078.=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0078.子集.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 8c68843d..139d0ed5 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -183,7 +183,28 @@ Python: Go: - +```Go +var res [][]int +func subset(nums []int) [][]int { + res = make([][]int, 0) + sort.Ints(nums) + Dfs([]int{}, nums, 0) + return res +} +func Dfs(temp, nums []int, start int){ + tmp := make([]int, len(temp)) + copy(tmp, temp) + res = append(res, tmp) + for i := start; i < len(nums); i++{ + //if i>start&&nums[i]==nums[i-1]{ + // continue + //} + temp = append(temp, nums[i]) + Dfs(temp, nums, i+1) + temp = temp[:len(temp)-1] + } +} +``` From b5af23699accc90fd4179daa1790a891b08a9edf Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 13:01:07 +0800 Subject: [PATCH 14/22] =?UTF-8?q?Update=200344.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0344.反转字符串.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index ddb9805d..c2f16794 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -146,6 +146,17 @@ Python: Go: +```Go +func reverseString(s []byte) { + left:=0 + right:=len(s)-1 + for left Date: Thu, 13 May 2021 13:30:55 +0800 Subject: [PATCH 15/22] update 0404 --- problems/0101.对称二叉树.md | 8 ++++-- problems/0110.平衡二叉树.md | 8 ++++-- problems/0404.左叶子之和.md | 45 +++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 561d0470..b9e203e8 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -253,9 +253,13 @@ public: ## 其他语言版本 - Java: +```java +``` + + + Python: @@ -284,4 +288,4 @@ const check = (leftPtr, rightPtr) => { * 作者微信:[程序员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 diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 5d55910c..ee215a68 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -353,9 +353,13 @@ public: ## 其他语言版本 - Java: +```java +``` + + + Python: @@ -369,4 +373,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 diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 8ff2b320..da4ed666 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -159,9 +159,51 @@ public: ## 其他语言版本 - Java: +**递归** + +```java +class Solution { + public int sumOfLeftLeaves(TreeNode root) { + if (root == null) return 0; + int leftValue = sumOfLeftLeaves(root.left); // 左 + int rightValue = sumOfLeftLeaves(root.right); // 右 + + int midValue = 0; + if (root.left != null && root.left.left == null && root.left.right == null) { // 中 + midValue = root.left.val; + } + int sum = midValue + leftValue + rightValue; + return sum; + } +} +``` + +**迭代** + +```java +class Solution { + public int sumOfLeftLeaves(TreeNode root) { + if (root == null) return 0; + Stack stack = new Stack<> (); + stack.add(root); + int result = 0; + while (!stack.isEmpty()) { + TreeNode node = stack.pop(); + if (node.left != null && node.left.left == null && node.left.right == null) { + result += node.left.val; + } + if (node.right != null) stack.add(node.right); + if (node.left != null) stack.add(node.left); + } + return result; + } +} +``` + + + Python: @@ -176,3 +218,4 @@ Go: * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
+ From fa1b935e8e8296fc44ee684b9f1f1944b2261564 Mon Sep 17 00:00:00 2001 From: Tiansheng Sui Date: Wed, 12 May 2021 22:42:44 -0700 Subject: [PATCH 16/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A00035.=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AEPython3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0035.搜索插入位置.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index c351e365..e891e3c5 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -237,6 +237,22 @@ class Solution { Python: +```python3 +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + + while left <= right: + middle = (left + right) // 2 + + if nums[middle] < target: + left = middle + 1 + elif nums[middle] > target: + right = middle - 1 + else: + return middle + return right + 1 +``` Go: From c3326eae14a66e42e6c007cdc8d2bed6f66d82c9 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 14:24:30 +0800 Subject: [PATCH 17/22] =?UTF-8?q?Update=200020.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index f2d78ade..ba50ad21 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -157,7 +157,26 @@ class Solution: ``` Go: +```Go +func isValid(s string) bool { + hash := map[byte]byte{')':'(', ']':'[', '}':'{'} + stack := make([]byte, 0) + if s == "" { + return true + } + for i := 0; i < len(s); i++ { + if s[i] == '(' || s[i] == '[' || s[i] == '{' { + stack = append(stack, s[i]) + } else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] { + stack = stack[:len(stack)-1] + } else { + return false + } + } + return len(stack) == 0 +} +``` From 0354702fb54961781601787e6afa994cf6cfd99f Mon Sep 17 00:00:00 2001 From: Chengkq <1395092352@qq.com> Date: Thu, 13 May 2021 14:29:08 +0800 Subject: [PATCH 18/22] =?UTF-8?q?update=200404=20=E5=B7=A6=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 5 ----- problems/0110.平衡二叉树.md | 5 ----- 2 files changed, 10 deletions(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index b9e203e8..535300af 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -255,11 +255,6 @@ public: Java: -```java -``` - - - Python: diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index ee215a68..3807aecc 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -355,11 +355,6 @@ public: Java: -```java -``` - - - Python: From b3168d09670dbb29007750d875c40b1f79f084e8 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Thu, 13 May 2021 18:04:01 +0800 Subject: [PATCH 19/22] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=E8=A1=A8=E8=BE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/回溯算法理论基础.md | 4 ---- problems/数组总结篇.md | 3 --- problems/数组理论基础.md | 3 --- 3 files changed, 10 deletions(-) diff --git a/problems/回溯算法理论基础.md b/problems/回溯算法理论基础.md index 3aba34db..8bfd101c 100644 --- a/problems/回溯算法理论基础.md +++ b/problems/回溯算法理论基础.md @@ -163,10 +163,6 @@ void backtracking(参数) { -![](https://img-blog.csdnimg.cn/20210416110157800.png) - - - ## 其他语言版本 diff --git a/problems/数组总结篇.md b/problems/数组总结篇.md index 7c4c0fed..d8daa866 100644 --- a/problems/数组总结篇.md +++ b/problems/数组总结篇.md @@ -131,9 +131,6 @@ 最后,大家周末愉快! - - - ## 其他语言版本 diff --git a/problems/数组理论基础.md b/problems/数组理论基础.md index 457cd33e..c36cc8f7 100644 --- a/problems/数组理论基础.md +++ b/problems/数组理论基础.md @@ -144,9 +144,6 @@ Java的二维数组可能是如下排列的方式: 数组专题中讲解和相关题目已经有16道了,就不介绍太过题目了,因为数组是非常基础的数据结构后面很多专题还会用到数组,所以后面的题目依然会会间接练习数组的。 - -![](https://img-blog.csdnimg.cn/20210416110157800.png) - ## 其他语言版本 From 3ca406df3aeb37f03719b73430e8ce00b4aec7cb Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 18:25:55 +0800 Subject: [PATCH 20/22] =?UTF-8?q?Update=200090.=E5=AD=90=E9=9B=86II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index cc5fd571..5782f55d 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -178,7 +178,30 @@ Python: Go: +```Go +var res[][]int +func subsetsWithDup(nums []int)[][]int { + res=make([][]int,0) + sort.Ints(nums) + dfs([]int{},nums,0) + return res +} +func dfs(temp, num []int, start int) { + tmp:=make([]int,len(temp)) + copy(tmp,temp) + + res=append(res,tmp) + for i:=start;istart&&num[i]==num[i-1]{ + continue + } + temp=append(temp,num[i]) + dfs(temp,num,i+1) + temp=temp[:len(temp)-1] + } +} +``` From e4087dfe785834a5e36e6df4ca08afa64ce52ef0 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 18:38:46 +0800 Subject: [PATCH 21/22] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index ec1335c6..f5626ea0 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -207,7 +207,7 @@ Java: Python: - +Go: ```Go func invertTree(root *TreeNode) *TreeNode { if root ==nil{ From bf7254925ad258cf2e62fa21e577b1c3416bb5cb Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Thu, 13 May 2021 21:24:03 +0800 Subject: [PATCH 22/22] Update --- problems/数组理论基础.md | 159 ++++++++++++++------------------- 1 file changed, 68 insertions(+), 91 deletions(-) diff --git a/problems/数组理论基础.md b/problems/数组理论基础.md index c36cc8f7..f061088f 100644 --- a/problems/数组理论基础.md +++ b/problems/数组理论基础.md @@ -7,6 +7,7 @@

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ ## 数组理论基础 数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力 @@ -23,6 +24,8 @@ ![算法通关数组](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%84.png) + + 需要两点注意的是 * **数组下标都是从0开始的。** @@ -34,6 +37,7 @@ ![算法通关数组1](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%841.png) + 而且大家如果使用C++的话,要注意vector 和 array的区别,vector的底层实现是array,严格来讲vector是容器,不是数组。 **数组的元素是不能删的,只能覆盖。** @@ -42,106 +46,79 @@ ![算法通关数组2](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%842.png) + **那么二维数组在内存的空间地址是连续的么?** -不同编程语言的内存管理是不一样的,以C++为例,在C++中二维数组是连续分布的,如图: +不同编程语言的内存管理是不一样的,以C++为例,在C++中二维数组是连续分布的。 + +我们来做一个实验,C++测试代码如下: + +```C++ +void test_arr() { + int array[2][3] = { + {0, 1, 2}, + {3, 4, 5} + }; + cout << &array[0][0] << " " << &array[0][1] << " " << &array[0][2] << endl; + cout << &array[1][0] << " " << &array[1][1] << " " << &array[1][2] << endl; +} + +int main() { + test_arr(); +} + +``` + +测试地址为 + +``` +0x7ffee4065820 0x7ffee4065824 0x7ffee4065828 +0x7ffee406582c 0x7ffee4065830 0x7ffee4065834 +``` + +注意地址为16进制,可以看出二维数组地址是连续一条线的。 + +一些录友可能看不懂内存地址,我就简单介绍一下, 0x7ffee4065820 与 0x7ffee4065824 差了一个4,就是4个字节,因为这是一个int型的数组,所以两个相信数组元素地址差4个字节。 + +0x7ffee4065828 与 0x7ffee406582c 也是差了4个字节,在16进制里8 + 4 = c,c就是12。 + +如图: ![数组内存](https://img-blog.csdnimg.cn/20210310150641186.png) -Java的二维数组可能是如下排列的方式: +**所以可以看出在C++中二维数组在地址空间上是连续的**。 + +像Java是没有指针的,同时也不对程序员暴漏其元素的地址,寻址操作完全交给虚拟机。 + +所以看不到每个元素的地址情况,这里我以Java为例,也做一个实验。 + +```Java +public static void test_arr() { + int[][] arr = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}, {9,9,9}}; + System.out.println(arr[0]); + System.out.println(arr[1]); + System.out.println(arr[2]); + System.out.println(arr[3]); +} +``` +输出的地址为: + +``` +[I@7852e922 +[I@4e25154f +[I@70dea4e +[I@5c647e05 +``` + +这里的数值也是16进制,这不是真正的地址,而是经过处理过后的数值了,我们也可以看出,二维数组的每一行头结点的地址是没有规则的,更谈不上连续。 + +所以Java的二维数组可能是如下排列的方式: ![算法通关数组3](https://img-blog.csdnimg.cn/20201214111631844.png) -我们在[数组过于简单,但你该了解这些!](https://mp.weixin.qq.com/s/c2KABb-Qgg66HrGf8z-8Og)分别作了实验 +这里面试中数组相关的理论知识就介绍完了。 -## 数组的经典题目 - -在面试中,数组是必考的基础数据结构。 - -其实数据的题目在思想上一般比较简单的,但是如果想高效,并不容易。 - -我们之前一共讲解了四道经典数组题目,每一道题目都代表一个类型,一种思想。 - -### 二分法 - -[704.二分查找](https://mp.weixin.qq.com/s/4X-8VRgnYRGd5LYGZ33m4w) - -在这道题目中我们讲到了**循环不变量原则**,只有在循环中坚持对区间的定义,才能清楚的把握循环中的各种细节。 - -**二分法是算法面试中的常考题,建议通过这道题目,锻炼自己手撕二分的能力**。 - -相关题目: - -* 35.搜索插入位置 -* 34.在排序数组中查找元素的第一个和最后一个位置 -* 69.x 的平方根 -* 367.有效的完全平方数 - -### 双指针法 - -[27. 移除元素](https://mp.weixin.qq.com/s/RMkulE4NIb6XsSX83ra-Ww) - -双指针法(快慢指针法):**通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。** - -暴力解法时间复杂度:O(n^2) -双指针时间复杂度:O(n) - -这道题目迷惑了不少同学,纠结于数组中的元素为什么不能删除,主要是因为一下两点: - -* 数组在内存中是连续的地址空间,不能释放单一元素,如果要释放,就是全释放(程序运行结束,回收内存栈空间)。 -* C++中vector和array的区别一定要弄清楚,vector的底层实现是array,所以vector展现出友好的一些都是因为经过包装了。 - -双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组和链表操作的面试题,都使用双指针法。 - -相关题目: - -* 26.删除排序数组中的重复项 -* 283.移动零 -* 844.比较含退格的字符串 -* 977.有序数组的平方 - -### 滑动窗口 - -[209.长度最小的子数组](https://mp.weixin.qq.com/s/ewCRwVw0h0v4uJacYO7htQ) - -本题介绍了数组操作中的另一个重要思想:滑动窗口。 - -暴力解法时间复杂度:O(n^2) -滑动窗口时间复杂度:O(n) - -本题中,主要要理解滑动窗口如何移动 窗口起始位置,达到动态更新窗口大小的,从而得出长度最小的符合条件的长度。 - -**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)的暴力解法降为O(n)。** - -如果没有接触过这一类的方法,很难想到类似的解题思路,滑动窗口方法还是很巧妙的。 - -相关题目: - -* 904.水果成篮 -* 76.最小覆盖子串 - -### 模拟行为 - -[59.螺旋矩阵II](https://mp.weixin.qq.com/s/Hn6-mlCPvKAdWbiFfQyaaw) - -模拟类的题目在数组中很常见,不涉及到什么算法,就是单纯的模拟,十分考察大家对代码的掌控能力。 - -在这道题目中,我们再一次介绍到了**循环不变量原则**,其实这也是写程序中的重要原则。 - -相信大家又遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,踩了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。 - -相关题目: - -* 54.螺旋矩阵 -* 剑指Offer 29.顺时针打印矩阵 - -## 总结 - -从二分法到双指针,从滑动窗口到螺旋矩阵,相信如果大家真的认真做了「代码随想录」每日推荐的题目,定会有所收获。 - -**每道题目后面都有相关练习题,也别忘了去做做!** - -数组专题中讲解和相关题目已经有16道了,就不介绍太过题目了,因为数组是非常基础的数据结构后面很多专题还会用到数组,所以后面的题目依然会会间接练习数组的。 +后续我将介绍面试中数组相关的五道经典面试题目,敬请期待! ## 其他语言版本