From 425593d78230462ede74c026fab8b119564c8ea1 Mon Sep 17 00:00:00 2001 From: bourne-3 <595962708@qq.com> Date: Mon, 6 Sep 2021 22:16:23 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0java=E5=AE=9E=E7=8E=B0143?= =?UTF-8?q?=E9=87=8D=E6=8E=92=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0143.重排链表.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index 76df63b7..62d4cb3f 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -219,6 +219,47 @@ public class ReorderList { return headNode.next; } } + +------------------------------------------------------------------------- +// 方法一 Java实现,使用数组存储节点 + class Solution { + public void reorderList(ListNode head) { + // 双指针的做法 + ListNode cur = head; + // ArrayList底层是数组,可以使用下标随机访问 + List list = new ArrayList<>(); + while (cur != null){ + list.add(cur); + cur = cur.next; + } + cur = head; // 重新回到头部 + int l = 1, r = list.size() - 1; // 注意左边是从1开始 + int count = 0; + while (l <= r){ + if (count % 2 == 0){ + // 偶数 + cur.next = list.get(r); + r--; + }else { + // 奇数 + cur.next = list.get(l); + l++; + } + // 每一次指针都需要移动 + cur = cur.next; + count++; + } + // 当是偶数的话,需要做额外处理 + if (list.size() % 2== 0){ + cur.next = list.get(l); + cur = cur.next; + } + + // 注意结尾要结束一波 + cur.next = null; + } +} + ``` Python: From cd7ef344e76cfb913256e65076de5d62cf47080c Mon Sep 17 00:00:00 2001 From: bourne-3 <595962708@qq.com> Date: Mon, 6 Sep 2021 22:36:27 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0java=E5=AE=9E=E7=8E=B0143?= =?UTF-8?q?=E9=87=8D=E6=8E=92=E9=93=BE=E8=A1=A8=E6=96=B9=E6=B3=95=E4=BA=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0143.重排链表.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index 62d4cb3f..2b4e68b7 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -177,6 +177,7 @@ public: Java: ```java +// 方法三 public class ReorderList { public void reorderList(ListNode head) { ListNode fast = head, slow = head; @@ -259,6 +260,35 @@ public class ReorderList { cur.next = null; } } +------------------------------------------------------------------------- +// 方法二:使用双端队列,简化了数组的操作,代码相对于前者更简洁(避免一些边界条件) +class Solution { + public void reorderList(ListNode head) { + // 使用双端队列的方法来解决 + Deque de = new LinkedList<>(); + // 这里是取head的下一个节点,head不需要再入队了,避免造成重复 + ListNode cur = head.next; + while (cur != null){ + de.offer(cur); + cur = cur.next; + } + cur = head; // 回到头部 + + int count = 0; + while (!de.isEmpty()){ + if (count % 2 == 0){ + // 偶数,取出队列右边尾部的值 + cur.next = de.pollLast(); + }else { + // 奇数,取出队列左边头部的值 + cur.next = de.poll(); + } + cur = cur.next; + count++; + } + cur.next = null; + } +} ``` From 41f2652d8677bbe97cd1fe7792415228f1032bb7 Mon Sep 17 00:00:00 2001 From: bourne-3 <595962708@qq.com> Date: Tue, 7 Sep 2021 12:13:27 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=C2=96129=E6=B1=82=E6=A0=B9=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E5=8F=B6=E8=8A=82=E7=82=B9=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E4=B9=8B=E5=92=8CJava=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0129.求根到叶子节点数字之和.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index 3439cd25..a330991e 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -164,6 +164,54 @@ public: Java: +```java +class Solution { + List path = new ArrayList<>(); + int res = 0; + + public int sumNumbers(TreeNode root) { + // 如果节点为0,那么就返回0 + if (root == null) return 0; + // 首先将根节点放到集合中 + path.add(root.val); + // 开始递归 + recur(root); + return res; + } + + public void recur(TreeNode root){ + if (root.left == null && root.right == null) { + // 当是叶子节点的时候,开始处理 + res += listToInt(path); + return; + } + + if (root.left != null){ + // 注意有回溯 + path.add(root.left.val); + recur(root.left); + path.remove(path.size() - 1); + } + if (root.right != null){ + // 注意有回溯 + path.add(root.right.val); + recur(root.right); + path.remove(path.size() - 1); + } + return; + } + + public int listToInt(List path){ + int sum = 0; + for (Integer num:path){ + // sum * 10 表示进位 + sum = sum * 10 + num; + } + return sum; + } +} +``` + Python: ```python3 class Solution: From 3d1843da4a89a2d0121bb3ebfc7365408fdae3b2 Mon Sep 17 00:00:00 2001 From: bourne-3 <595962708@qq.com> Date: Tue, 7 Sep 2021 12:20:01 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=C2=96129=E6=B1=82=E6=A0=B9=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E5=8F=B6=E8=8A=82=E7=82=B9=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E4=B9=8B=E5=92=8CJava=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0129.求根到叶子节点数字之和.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index a330991e..7f50c885 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -168,7 +168,6 @@ Java: class Solution { List path = new ArrayList<>(); int res = 0; - public int sumNumbers(TreeNode root) { // 如果节点为0,那么就返回0 if (root == null) return 0; @@ -200,7 +199,6 @@ class Solution { } return; } - public int listToInt(List path){ int sum = 0; for (Integer num:path){