From b3ac4e3b2a20e9063c8294a40720a062d8c1f1b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Thu, 19 Aug 2021 16:33:56 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2024.=20=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=20Swift=E7=89=88=E6=9C=AC?= 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 91e566dd..55a6bb50 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -248,6 +248,27 @@ fun swapPairs(head: ListNode?): ListNode? { } ``` +Swift: +```swift +func swapPairs(_ head: ListNode?) -> ListNode? { + if head == nil || head?.next == nil { + return head + } + let dummyHead: ListNode = ListNode(-1, head) + var current: ListNode? = dummyHead + while current?.next != nil && current?.next?.next != nil { + let temp1 = current?.next + let temp2 = current?.next?.next?.next + + current?.next = current?.next?.next + current?.next?.next = temp1 + current?.next?.next?.next = temp2 + + current = current?.next?.next + } + return dummyHead.next +} +``` ----------------------- From 3e754148100b7bf5894e9ab543121613fb0c8cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Fri, 20 Aug 2021 13:37:18 +0800 Subject: [PATCH 2/6] =?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=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 3e1a682c..4d3e57db 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -204,6 +204,31 @@ fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { } ``` +Swift: +```swift +func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { + if head == nil { + return nil + } + if n == 0 { + return head + } + let dummyHead = ListNode(-1, head) + var fast: ListNode? = dummyHead + var slow: ListNode? = dummyHead + // fast 前移 n + for _ in 0 ..< n { + fast = fast?.next + } + while fast?.next != nil { + fast = fast?.next + slow = slow?.next + } + slow?.next = slow?.next?.next + return dummyHead.next +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 0498cf3935ead6fdf06495e00004ed8e6f2146e3 Mon Sep 17 00:00:00 2001 From: binglu18 <57309010+binglu18@users.noreply.github.com> Date: Fri, 20 Aug 2021 15:33:59 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=A1=A5=E5=85=850102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md=20515?= =?UTF-8?q?=E9=A2=98java=E8=A7=A3=E6=B3=95=E4=BA=8C?= 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..4a26d853 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1070,6 +1070,29 @@ class Solution { return retVal; } } + +方法二:用一个max变量来保存最大值 +class Solution { + public List largestValues(TreeNode root) { + Queue queue = new LinkedList(); + List result = new ArrayList<>(); + if (root!=null) queue.add(root); + while(!queue.isEmpty()){ + int size = queue.size(); + int max = Integer.MIN_VALUE; // 初始化为最小值 + for(int i=0;i Date: Fri, 20 Aug 2021 15:37:35 +0800 Subject: [PATCH 4/6] =?UTF-8?q?update=20=E6=96=B9=E6=B3=95=E4=BA=8C=200102?= =?UTF-8?q?.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86.md=20515=E9=A2=98java=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 4a26d853..15d00af2 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1070,8 +1070,10 @@ class Solution { return retVal; } } +``` -方法二:用一个max变量来保存最大值 +```java +//方法二:用一个max变量来保存最大值 class Solution { public List largestValues(TreeNode root) { Queue queue = new LinkedList(); From 07d27158b238812f603f250794b9a681f71e7f8e Mon Sep 17 00:00:00 2001 From: binglu18 <57309010+binglu18@users.noreply.github.com> Date: Fri, 20 Aug 2021 16:10:24 +0800 Subject: [PATCH 5/6] =?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=20515java?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=BA=8Cformat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 15d00af2..d0f584fe 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1078,16 +1078,17 @@ class Solution { public List largestValues(TreeNode root) { Queue queue = new LinkedList(); List result = new ArrayList<>(); - if (root!=null) queue.add(root); + if (root != null) queue.add(root); + while(!queue.isEmpty()){ int size = queue.size(); int max = Integer.MIN_VALUE; // 初始化为最小值 - for(int i=0;i Date: Sat, 21 Aug 2021 17:21:06 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=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=E4=B8=AD=E7=9A=84=20=20111.=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=20=E7=9A=84java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index dde5af47..b9bc05e0 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1597,6 +1597,43 @@ public: ``` Java: +```java +class Solution { + + public int minDepth(TreeNode root){ + if (root == null) { + return 0; + } + + Queue queue = new LinkedList<>(); + queue.offer(root); + int depth = 0; + + while (!queue.isEmpty()){ + + int size = queue.size(); + depth++; + + TreeNode cur = null; + for (int i = 0; i < size; i++) { + cur = queue.poll(); + + //如果当前节点的左右孩子都为空,直接返回最小深度 + if (cur.left == null && cur.right == null){ + return depth; + } + + if (cur.left != null) queue.offer(cur.left); + if (cur.right != null) queue.offer(cur.right); + } + + + } + + return depth; + } +} +``` Python 3: