From d2cc6c49da0d56e6036ebfbd686dc573eccbbf09 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Thu, 5 Aug 2021 23:14:59 +0800 Subject: [PATCH 01/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?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 添加 0406.根据身高重建队列 go版本 --- problems/0406.根据身高重建队列.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index a5f66a5d..699f67d0 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -217,7 +217,25 @@ class Solution: ``` Go: - +```golang +func reconstructQueue(people [][]int) [][]int { + //先将身高从大到小排序,确定最大个子的相对位置 + sort.Slice(people,func(i,j int)bool{ + if people[i][0]==people[j][0]{ + return people[i][1]people[j][0]//这个只是确保身高按照由大到小的顺序来排,并不确定K是按照从小到大排序的 + }) + //再按照K进行插入排序,优先插入K小的 + result := make([][]int, 0) + for _, info := range people { + result = append(result, info) + copy(result[info[1] +1:], result[info[1]:])//将插入位置之后的元素后移动一位(意思是腾出空间) + result[info[1]] = info//将插入元素位置插入元素 + } + return result +} +``` Javascript: ```Javascript var reconstructQueue = function(people) { From b3cf5d46f513cfd1e8ee93067a25a98b4f244c2e Mon Sep 17 00:00:00 2001 From: wjjiang <48505670+Spongecaptain@users.noreply.github.com> Date: Fri, 6 Aug 2021 09:48:40 +0800 Subject: [PATCH 02/23] =?UTF-8?q?Update=200617.=E5=90=88=E5=B9=B6=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 添加 Go 的迭代版本 --- problems/0617.合并二叉树.md | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 09d844f3..2900a817 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -426,6 +426,46 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode { root1.Right = mergeTrees(root1.Right, root2.Right) return root1 } + +// 迭代版本 +func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode { + queue := make([]*TreeNode,0) + if root1 == nil{ + return root2 + } + if root2 == nil{ + return root1 + } + queue = append(queue,root1) + queue = append(queue,root2) + + for size:=len(queue);size>0;size=len(queue){ + node1 := queue[0] + queue = queue[1:] + node2 := queue[0] + queue = queue[1:] + node1.Val += node2.Val + // 左子树都不为空 + if node1.Left != nil && node2.Left != nil{ + queue = append(queue,node1.Left) + queue = append(queue,node2.Left) + } + // 右子树都不为空 + if node1.Right !=nil && node2.Right !=nil{ + queue = append(queue,node1.Right) + queue = append(queue,node2.Right) + } + // 树 1 的左子树为 nil,直接接上树 2 的左子树 + if node1.Left == nil{ + node1.Left = node2.Left + } + // 树 1 的右子树为 nil,直接接上树 2 的右子树 + if node1.Right == nil{ + node1.Right = node2.Right + } + } + return root1 +} ``` JavaScript: From 18bf14707c3b75148a07e4c82110df2b07eab5ae Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Fri, 6 Aug 2021 10:56:21 +0800 Subject: [PATCH 03/23] =?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=AE=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pics/.DS_Store | Bin 6148 -> 0 bytes problems/0035.搜索插入位置.md | 41 ++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) delete mode 100644 pics/.DS_Store diff --git a/pics/.DS_Store b/pics/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Int { + for i in 0..= target { + return i + } + } + return nums.count +} + +// 二分法 +func searchInsert(_ nums: [Int], _ target: Int) -> Int { + var left = 0 + var right = nums.count - 1 + + while left <= right { + let middle = left + ((right - left) >> 1) + + if nums[middle] > target { + right = middle - 1 + }else if nums[middle] < target { + left = middle + 1 + }else if nums[middle] == target { + return middle + } + } + + return right + 1 +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 0f6ffd0d1bf7e968b8f730785eb4f3c083e7dfea Mon Sep 17 00:00:00 2001 From: Nixiak-nan <70318059+Nixiak-nan@users.noreply.github.com> Date: Fri, 6 Aug 2021 13:40:01 +0800 Subject: [PATCH 04/23] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加题目地址 --- problems/0513.找树左下角的值.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 27c6e83c..7e48b53c 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -9,6 +9,8 @@ ## 513.找树左下角的值 +题目地址:[https://leetcode-cn.com/problems/find-bottom-left-tree-value/](https://leetcode-cn.com/problems/find-bottom-left-tree-value/v) + 给定一个二叉树,在树的最后一行找到最左边的值。 示例 1: From 1ae78a86ca73cf229acbbfe8fa6d16a505814e34 Mon Sep 17 00:00:00 2001 From: hitorig <86967184+hitorig@users.noreply.github.com> Date: Fri, 6 Aug 2021 17:43:34 +0800 Subject: [PATCH 05/23] =?UTF-8?q?Update=200046.=E5=85=A8=E6=8E=92=E5=88=97?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 3ee7271e..3b3ba180 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -227,24 +227,27 @@ class Solution: Go: ```Go -var result [][]int -func backtrack(nums,pathNums []int,used []bool){ - if len(nums)==len(pathNums){ - tmp:=make([]int,len(nums)) - copy(tmp,pathNums) - result=append(result,tmp) - //result=append(result,pathNums) - return - } - for i:=0;i Date: Fri, 6 Aug 2021 18:03:29 +0800 Subject: [PATCH 06/23] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92=E5=88=97?= =?UTF-8?q?II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 48 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 66107c95..c8764241 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -228,35 +228,31 @@ Go: ```go var res [][]int func permute(nums []int) [][]int { - res = [][]int{} - sort.Ints(nums) - dfs(nums, make([]int, 0), make([]bool, len(nums))) - return res + res = [][]int{} + backTrack(nums,len(nums),[]int{}) + return res } +func backTrack(nums []int,numsLen int,path []int) { + if len(nums)==0{ + p:=make([]int,len(path)) + copy(p,path) + res = append(res,p) + } + used := [21]int{}//跟前一题唯一的区别,同一层不使用重复的数。关于used的思想carl在递增子序列那一题中提到过 + for i:=0;i Date: Sat, 7 Aug 2021 08:28:16 +0800 Subject: [PATCH 07/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2024=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index b285b2d4..cae69cea 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -228,6 +228,27 @@ var swapPairs = function (head) { }; ``` +Kotlin: + +```kotlin +fun swapPairs(head: ListNode?): ListNode? { + val dummyNode = ListNode(0).apply { + this.next = head + } + var cur: ListNode? = dummyNode + while (cur?.next != null && cur.next?.next != null) { + val temp = cur.next + val temp2 = cur.next?.next?.next + cur.next = cur.next?.next + cur.next?.next = temp + cur.next?.next?.next = temp2 + cur = cur.next?.next + } + return dummyNode.next +} +``` + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 3caaf866f4e2ec8ce846b261dbf5b07b1a828aa4 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sat, 7 Aug 2021 10:25:15 +0800 Subject: [PATCH 08/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201002.=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6python3=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/1002.查找常用字符.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index e3014d00..19f9e128 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -169,6 +169,29 @@ class Solution { } } ``` +```python +class Solution: + def commonChars(self, words: List[str]) -> List[str]: + if not words: return [] + result = [] + hash = [0] * 26 # 用来统计所有字符串里字符出现的最小频率 + for i, c in enumerate(words[0]): # 用第一个字符串给hash初始化 + hash[ord(c) - ord('a')] += 1 + # 统计除第一个字符串外字符的出现频率 + for i in range(1, len(words)): + hashOtherStr = [0] * 26 + for j in range(len(words[0])): + hashOtherStr[ord(words[i][j]) - ord('a')] += 1 + # 更新hash,保证hash里统计26个字符在所有字符串里出现的最小次数 + for k in range(26): + hash[k] = min(hash[k], hashOtherStr[k]) + # 将hash统计的字符次数,转成输出形式 + for i in range(26): + while hash[i] != 0: # 注意这里是while,多个重复的字符 + result.extend(chr(i + ord('a'))) + hash[i] -= 1 + return result +``` javaScript ```js var commonChars = function (words) { From cb65921f6cb9f6c983af9ff2f8c02f75f6f4d1d4 Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 10:55:06 +0800 Subject: [PATCH 09/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20707=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 349e2409..f7b03c8b 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -880,7 +880,72 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { * obj.deleteAtIndex(index) */ ``` +Kotlin: +```kotlin +class MyLinkedList { + var next: ListNode? = null + + var size: Int = 0 + + fun get(index: Int): Int { + if (index + 1 > size) return -1 + var cur = this.next + for (i in 0 until index) { + cur = cur?.next + } + return cur?.`val` ?: -1 + } + + fun addAtHead(`val`: Int) { + val head = ListNode(`val`) + head.next = this.next + this.next = head + size++ + } + + fun addAtTail(`val`: Int) { + val pre = ListNode(0) + pre.next = this.next + var cur: ListNode? = pre + while (cur?.next != null) { + cur = cur.next + } + cur?.next = ListNode(`val`) + this.next = pre.next + size++ + } + + fun addAtIndex(index: Int, `val`: Int) { + if (index == 0) return addAtHead(`val`) + if (index == size) return addAtTail(`val`) + if (index > size) return + var cur = this.next + for (i in 0 until index - 1) { + cur = cur?.next + } + val temp = cur?.next + cur?.next = ListNode(`val`) + cur?.next?.next = temp + size++ + } + + fun deleteAtIndex(index: Int) { + if (index + 1 > size) return + val pre = ListNode(0) + pre.next = this.next + var cur: ListNode? = pre + for (i in 0 until index) { + cur = cur?.next + } + val temp = cur?.next?.next + cur?.next?.next = null + cur?.next = temp + this.next = pre.next + size-- + } +} +``` From 91c6ecdee95f5eb97b53590a81e242ef6d5f75c7 Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 11:04:11 +0800 Subject: [PATCH 10/23] =?UTF-8?q?=E4=BC=98=E5=8C=96=20707=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8=20addAtIndex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index f7b03c8b..5e748a24 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -917,16 +917,17 @@ class MyLinkedList { } fun addAtIndex(index: Int, `val`: Int) { - if (index == 0) return addAtHead(`val`) - if (index == size) return addAtTail(`val`) if (index > size) return - var cur = this.next - for (i in 0 until index - 1) { + val pre = ListNode(0) + pre.next = this.next + var cur:ListNode? = pre + for (i in 0 until index) { cur = cur?.next } val temp = cur?.next cur?.next = ListNode(`val`) cur?.next?.next = temp + this.next = pre.next size++ } From adddd296c50504d1a1756aab6d3f15d9f8ae4fc1 Mon Sep 17 00:00:00 2001 From: Nixiak-nan <70318059+Nixiak-nan@users.noreply.github.com> Date: Sat, 7 Aug 2021 11:20:47 +0800 Subject: [PATCH 11/23] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0236.二叉树的最近公共祖先 java版本中有一个变量没用,删除该变量 --- problems/0236.二叉树的最近公共祖先.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 7b5deb56..41e12df4 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -249,7 +249,6 @@ class Solution { ```java // 代码精简版 class Solution { - TreeNode pre; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root.val == p.val ||root.val == q.val) return root; TreeNode left = lowestCommonAncestor(root.left,p,q); From 9dd846ec55f687a701631ff1a311e340283793bb Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 11:39:11 +0800 Subject: [PATCH 12/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20206=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 4efdda3b..27a2e903 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -319,7 +319,20 @@ def reverse(pre, cur) reverse(cur, tem) # 通过递归实现双指针法中的更新操作 end ``` - +Kotlin: +```Kotlin +fun reverseList(head: ListNode?): ListNode? { + var pre: ListNode? = null + var cur = head + while (cur != null) { + val temp = cur.next + cur.next = pre + pre = cur + cur = temp + } + return pre +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 0f4c80a935bdbc64bbcc594f5507ce5aa5da9e53 Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 12:11:40 +0800 Subject: [PATCH 13/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2019=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 52735794..4b17c972 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -184,6 +184,25 @@ var removeNthFromEnd = function(head, n) { return ret.next; }; ``` +Kotlin: +```Kotlin +fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { + val pre = ListNode(0).apply { + this.next = head + } + var fastNode: ListNode? = pre + var slowNode: ListNode? = pre + for (i in 0..n) { + fastNode = fastNode?.next + } + while (fastNode != null) { + slowNode = slowNode?.next + fastNode = fastNode.next + } + slowNode?.next = slowNode?.next?.next + return pre.next +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From a0fa0ebde67ac87ed2f661f8ac3637f981060b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=9D=9C=E6=9E=97?= Date: Sat, 7 Aug 2021 14:42:17 +0800 Subject: [PATCH 14/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=20Swift=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pics/.DS_Store | Bin 0 -> 6148 bytes problems/0027.移除元素.md | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 pics/.DS_Store diff --git a/pics/.DS_Store b/pics/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Int { + var slowIndex = 0 + + for fastIndex in 0.. Date: Sat, 7 Aug 2021 21:15:06 +0800 Subject: [PATCH 15/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201207.=E7=8B=AC?= =?UTF-8?q?=E4=B8=80=E6=97=A0=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0=E6=AC=A1?= =?UTF-8?q?=E6=95=B0python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1207.独一无二的出现次数.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index c1720430..32aa3c3a 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -100,7 +100,21 @@ class Solution { ``` Python: - +```python +class Solution: + def uniqueOccurrences(self, arr: List[int]) -> bool: + count = [0] * 2002 + for i in range(len(arr)): + count[arr[i] + 1000] += 1 # 防止负数作为下标 + freq = [False] * 1002 # 标记相同频率是否重复出现 + for i in range(2001): + if count[i] > 0: + if freq[count[i]] == False: + freq[count[i]] = True + else: + return False + return True +``` Go: JavaScript: From 0076ddab1998c3846430eb436ebfc7e4ff23a075 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sun, 8 Aug 2021 08:54:35 +0800 Subject: [PATCH 16/23] =?UTF-8?q?=E4=BF=AE=E6=AD=A30349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86python3=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/0349.两个数组的交集.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 5c635d39..5dc18478 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -127,7 +127,7 @@ class Solution: for num in nums2: if num in set1: result_set.add(num) # set1里出现的nums2元素 存放到结果 - return result_set + return list(result_set) ``` From d300a40ac51e8fb8df8802c27d0a81dc94b0e6ee Mon Sep 17 00:00:00 2001 From: wjjiang <48505670+Spongecaptain@users.noreply.github.com> Date: Sun, 8 Aug 2021 13:48:00 +0800 Subject: [PATCH 17/23] =?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 添加 Go 语言迭代版本 --- .../0450.删除二叉搜索树中的节点.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 4695ed50..e6ce469e 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -320,6 +320,7 @@ class Solution: Go: ```Go +// 递归版本 func deleteNode(root *TreeNode, key int) *TreeNode { if root==nil{ return nil @@ -356,6 +357,51 @@ func deleteNode1(root *TreeNode)*TreeNode{ root.Left=deleteNode1(root.Left) return root } +// 迭代版本 +func deleteOneNode(target *TreeNode) *TreeNode { + if target == nil { + return target + } + if target.Right == nil { + return target.Left + } + cur := target.Right + for cur.Left != nil { + cur = cur.Left + } + cur.Left = target.Left + return target.Right +} +func deleteNode(root *TreeNode, key int) *TreeNode { + // 特殊情况处理 + if root == nil { + return root + } + cur := root + var pre *TreeNode + for cur != nil { + if cur.Val == key { + break + } + pre = cur + if cur.Val > key { + cur = cur.Left + } else { + cur = cur.Right + } + } + if pre == nil { + return deleteOneNode(cur) + } + // pre 要知道是删除左孩子还有右孩子 + if pre.Left != nil && pre.Left.Val == key { + pre.Left = deleteOneNode(cur) + } + if pre.Right != nil && pre.Right.Val == key { + pre.Right = deleteOneNode(cur) + } + return root +} ``` JavaScript版本 From ee54db5b5dbbac4196b94e7f007a7f4cd3ddc8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=9D=9C=E6=9E=97?= Date: Sun, 8 Aug 2021 16:51:19 +0800 Subject: [PATCH 18/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A00977.=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20Swift=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/0977.有序数组的平方.md | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 0f9007d7..7206fd6f 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -224,6 +224,36 @@ const sortedSquares = function (nums) { } ``` +Swift: + +```swift +func sortedSquares(_ nums: [Int]) -> [Int] { + // 指向新数组最后一个元素 + var k = nums.count - 1 + // 指向原数组第一个元素 + var i = 0 + // 指向原数组最后一个元素 + var j = nums.count - 1 + // 初始化新数组(用-1填充) + var result = Array(repeating: -1, count: nums.count) + + for _ in 0.. Date: Sun, 8 Aug 2021 23:08:58 +0800 Subject: [PATCH 19/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200452.=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?= =?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0452.用最少数量的箭引爆气球 go版本 --- .../0452.用最少数量的箭引爆气球.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index bb3ebbdc..a4db9e88 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -175,6 +175,30 @@ class Solution: Go: +```golang +func findMinArrowShots(points [][]int) int { + var res int =1//弓箭数 + //先按照第一位排序 + sort.Slice(points,func (i,j int) bool{ + return points[i][0]b{ + return b + } + return a +} +``` Javascript: ```Javascript var findMinArrowShots = function(points) { From 54733e33ac7a303f4295889589f7340913f114c2 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 8 Aug 2021 23:33:31 +0800 Subject: [PATCH 20/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200435.=E6=97=A0?= =?UTF-8?q?=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0435.无重叠区间 go版本 --- problems/0435.无重叠区间.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index d32c2ebb..b86f6714 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -250,7 +250,29 @@ class Solution: ``` Go: - +```golang +func eraseOverlapIntervals(intervals [][]int) int { + var flag int + //先排序 + sort.Slice(intervals,func(i,j int)bool{ + return intervals[i][0]intervals[i][0]{ + flag++ + intervals[i][1]=min(intervals[i-1][1],intervals[i][1])//由于是先排序的,所以,第一位是递增顺序,故只需要将临近两个元素的第二个值最小值更新到该元素的第二个值即可作之后的判断 + } + } + return flag +} +func min(a,b int)int{ + if a>b{ + return b + } + return a +} +``` Javascript: ```Javascript var eraseOverlapIntervals = function(intervals) { From fba1fa33d6068f42c255e0ed3c9287594208f0ea Mon Sep 17 00:00:00 2001 From: Jack <965555169@qq.com> Date: Mon, 9 Aug 2021 09:01:41 +0800 Subject: [PATCH 21/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=20III-JavaScript=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0123.买卖股票的最佳时机III.md | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 7ff1bfe2..c7193bbc 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -278,7 +278,44 @@ class Solution: return dp[4] ``` -Go: +JavaScript: + +> 版本一: + +```javascript +const maxProfit = prices => { + const len = prices.length; + const dp = new Array(len).fill(0).map(x => new Array(5).fill(0)); + dp[0][1] = -prices[0]; + dp[0][3] = -prices[0]; + for (let i = 1; i < len; i++) { + dp[i][0] = dp[i - 1][0]; + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); + dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]); + dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]); + dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]); + } + return dp[len - 1][4]; +}; +``` + +> 版本二: + +```javascript +const maxProfit = prices => { + const len = prices.length; + const dp = new Array(5).fill(0); + dp[1] = -prices[0]; + dp[3] = -prices[0]; + for (let i = 1; i < len; i++) { + dp[1] = Math.max(dp[1], dp[0] - prices[i]); + dp[2] = Math.max(dp[2], dp[1] + prices[i]); + dp[3] = Math.max(dp[3], dp[2] - prices[i]); + dp[4] = Math.max(dp[4], dp[3] + prices[i]); + } + return dp[4]; +}; +``` From adcfdd63d6c5c447c6cccb81f9dae696a05a3795 Mon Sep 17 00:00:00 2001 From: Jack <965555169@qq.com> Date: Mon, 9 Aug 2021 10:12:06 +0800 Subject: [PATCH 22/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A0116.=20=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?= =?UTF-8?q?=E9=92=88-JavaScript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...个节点的下一个右侧节点指针.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 28e6f645..6feb1ca9 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -130,21 +130,47 @@ public: ## Java ```java + ``` ## Python ```python + ``` ## Go ```go + ``` ## JavaScript ```js +const connect = root => { + if (!root) return root; + // 根节点入队 + const Q = [root]; + while (Q.length) { + const len = Q.length; + // 遍历这一层的所有节点 + for (let i = 0; i < len; i++) { + // 队头出队 + const node = Q.shift(); + // 连接 + if (i < len - 1) { + // 新的队头是node的右边元素 + node.next = Q[0]; + } + // 队头左节点有值,放入队列 + node.left && Q.push(node.left); + // 队头右节点有值,放入队列 + node.right && Q.push(node.right); + } + } + return root; +}; ``` ----------------------- From 422dd1bcc1a3cd8ec38abdf00054894e556e2246 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Mon, 9 Aug 2021 22:07:43 +0800 Subject: [PATCH 23/23] =?UTF-8?q?=E6=B7=BB=E5=8A=A01365.=E6=9C=89=E5=A4=9A?= =?UTF-8?q?=E5=B0=91=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E5=AD=97python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1365.有多少小于当前数字的数字.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 6324329c..30241c9b 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -139,7 +139,19 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ``` Python: - +```python +class Solution: + def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: + res = nums[:] + hash = dict() + res.sort() # 从小到大排序之后,元素下标就是小于当前数字的数字 + for i, num in enumerate(res): + if num not in hash.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况 + hash[num] = i + for i, num in enumerate(nums): + res[i] = hash[num] + return res +``` Go: JavaScript: