From cb9175f41a672e321cdd13ec5ba4a0fa7e3066da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=9D=9C=E6=9E=97?= Date: Tue, 17 Aug 2021 22:53:03 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 0aa038e8..0b8cd2e4 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -948,6 +948,87 @@ class MyLinkedList { } ``` +Swift: + +```swift +class MyLinkedList { + var dummyHead: ListNode? + var size: Int + + init() { + dummyHead = ListNode(0) + size = 0 + } + + func get(_ index: Int) -> Int { + if index >= size || index < 0 { + return -1 + } + + var curNode = dummyHead?.next + var curIndex = index + + while curIndex > 0 { + curNode = curNode?.next + curIndex -= 1 + } + + return curNode?.value ?? -1 + } + + func addAtHead(_ val: Int) { + let newHead = ListNode(val) + newHead.next = dummyHead?.next + dummyHead?.next = newHead + size += 1 + } + + func addAtTail(_ val: Int) { + let newNode = ListNode(val) + var curNode = dummyHead + while curNode?.next != nil { + curNode = curNode?.next + } + + curNode?.next = newNode + size += 1 + } + + func addAtIndex(_ index: Int, _ val: Int) { + if index > size { + return + } + + let newNode = ListNode(val) + var curNode = dummyHead + var curIndex = index + + while curIndex > 0 { + curNode = curNode?.next + curIndex -= 1 + } + + newNode.next = curNode?.next + curNode?.next = newNode + size += 1 + } + + func deleteAtIndex(_ index: Int) { + if index >= size || index < 0 { + return + } + + var curNode = dummyHead + for _ in 0.. Date: Wed, 18 Aug 2021 01:26:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0f9b2df6..9b1435a2 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1129,7 +1129,7 @@ var largestValues = function(root) { queue.push(root); while(root!==null&&queue.length){ //设置max初始值就是队列的第一个元素 - let max=queue[0]; + let max=queue[0].val; let length=queue.length; while(length--){ let node = queue.shift(); From 4b71311621baad2dc356dbe100e608435cc47bd8 Mon Sep 17 00:00:00 2001 From: shuwen Date: Thu, 19 Aug 2021 12:41:16 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=B1=82=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E4=B8=AD=E7=9A=84=20104.=20=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?=20Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0f9b2df6..dc386684 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1532,6 +1532,29 @@ Java: Python: +```python 3 +class Solution: + def maxDepth(self, root: TreeNode) -> int: + if root == None: + return 0 + + queue_ = [root] + result = [] + while queue_: + length = len(queue_) + sub = [] + for i in range(length): + cur = queue_.pop(0) + sub.append(cur.val) + #子节点入队列 + if cur.left: queue_.append(cur.left) + if cur.right: queue_.append(cur.right) + result.append(sub) + + + return len(result) +``` + Go: From bd3d65c41530e2f8268986ce9f4b13d889d5925d Mon Sep 17 00:00:00 2001 From: DoubleYellowIce <65336599+DoubleYellowIce@users.noreply.github.com> Date: Thu, 19 Aug 2021 14:34:15 +0800 Subject: [PATCH 4/5] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAIV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0188.买卖股票的最佳时机IV.md | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 9fe7a919..66676a7a 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -196,7 +196,7 @@ class Solution { } } -// 版本二: 空间优化 +// 版本二: 二维 dp数组 class Solution { public int maxProfit(int k, int[] prices) { if (prices.length == 0) return 0; @@ -220,6 +220,25 @@ class Solution { return dp[len - 1][k*2]; } } + +//版本三:一维 dp数组 +class Solution { + public int maxProfit(int k, int[] prices) { + //在版本二的基础上,由于我们只关心前一天的股票买入情况,所以只存储前一天的股票买入情况 + if(prices.length==0)return 0; + int[] dp=new int[2*k+1]; + for (int i = 1; i <2*k ; i+=2) { + dp[i]=-prices[0]; + } + for (int i = 0; i Date: Thu, 19 Aug 2021 17:00:42 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20=20=E5=85=B3=E4=BA=8E111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=E7=9A=84Python?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 32 ++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index dc386684..109ed70f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1562,6 +1562,8 @@ JavaScript: # 111.二叉树的最小深度 +题目地址:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ + 相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。 **需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点** @@ -1597,7 +1599,35 @@ public: Java: -Python: +Python 3: + +```python 3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def minDepth(self, root: TreeNode) -> int: + if root == None: + return 0 + + #根节点的深度为1 + queue_ = [(root,1)] + while queue_: + cur, depth = queue_.pop(0) + + if cur.left == None and cur.right == None: + return depth + #先左子节点,由于左子节点没有孩子,则就是这一层了 + if cur.left: + queue_.append((cur.left,depth + 1)) + if cur.right: + queue_.append((cur.right,depth + 1)) + + return 0 +``` Go: