From 7a1d070c257c16bf01aaf4b95bded65393bc3286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=AE=A3=E9=AD=81?= <13963268+cheng-xuankui@user.noreply.gitee.com> Date: Thu, 26 Dec 2024 20:49:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A019.=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,=E6=B7=BB=E5=8A=A0=E4=BA=86java?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BD=BF=E7=94=A8=E9=80=92=E5=BD=92=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=88=A0=E9=99=A4=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index fafef1f2..16312d0f 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -129,6 +129,36 @@ class Solution { } ``` + +```java +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + // 创建一个新的哑节点,指向原链表头 + ListNode s = new ListNode(-1, head); + // 递归调用remove方法,从哑节点开始进行删除操作 + remove(s, n); + // 返回新链表的头(去掉可能的哑节点) + return s.next; + } + + public int remove(ListNode p, int n) { + // 递归结束条件:如果当前节点为空,返回0 + if (p == null) { + return 0; + } + // 递归深入到下一个节点 + int net = remove(p.next, n); + // 如果当前节点是倒数第n个节点,进行删除操作 + if (net == n) { + p.next = p.next.next; + } + // 返回当前节点的总深度 + return net + 1; + } +} +``` + + ### Python: ```python From b72609d65ed2c41212ba39bba57f21615be6077e Mon Sep 17 00:00:00 2001 From: Jian Date: Sat, 4 Jan 2025 01:59:28 +0800 Subject: [PATCH 2/4] =?UTF-8?q?0018=E5=9B=9B=E6=95=B0=E4=B9=8B=E5=92=8C=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=80=9D=E8=B7=AF=E7=AC=AC=E4=BA=8C=E6=AE=B5?= =?UTF-8?q?=E6=9C=AB=E5=B0=BE=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 64923e41..fb8557fa 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -35,7 +35,7 @@ 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 -但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[i] > target && (nums[i] >=0 || target >= 0)`就可以了。 +但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[k] > target && (nums[k] >=0 || target >= 0)`就可以了。 [15.三数之和](https://programmercarl.com/0015.三数之和.html)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。 @@ -802,3 +802,4 @@ end + From d5e0827abeabbcc97b341e99231966bfbafaf7cd Mon Sep 17 00:00:00 2001 From: Jian Date: Wed, 8 Jan 2025 01:04:07 +0800 Subject: [PATCH 3/4] =?UTF-8?q?0020=E6=9C=89=E6=95=88=E7=9A=84=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7=20Java=E6=9B=B4=E7=AE=80=E5=8D=95=E6=98=93=E6=87=82?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index f2f5cdd1..493e2871 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -172,6 +172,29 @@ class Solution { } ``` +```java +// 解法二 +// 对应的另一半一定在栈顶 +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + for(char c : s.toCharArray()){ + // 有对应的另一半就直接消消乐 + if(c == ')' && !stack.isEmpty() && stack.peek() == '(') + stack.pop(); + else if(c == '}' && !stack.isEmpty() && stack.peek() == '{') + stack.pop(); + else if(c == ']' && !stack.isEmpty() && stack.peek() == '[') + stack.pop(); + else + stack.push(c);// 没有匹配的就放进去 + } + + return stack.isEmpty(); + } +} +``` + ### Python: ```python From a66142cbc57e9f3fbe3d5b72689d5cab38fbc690 Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 17 Jan 2025 19:37:21 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9239=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BCjava=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=B3=A8=E9=87=8A=E4=B8=A5=E9=87=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index caa24d8d..63ee3dae 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -267,7 +267,7 @@ class Solution { //利用双端队列手动实现单调队列 /** * 用一个单调队列来存储对应的下标,每当窗口滑动的时候,直接取队列的头部指针对应的值放入结果集即可 - * 单调队列类似 (tail -->) 3 --> 2 --> 1 --> 0 (--> head) (右边为头结点,元素存的是下标) + * 单调递减队列类似 (head -->) 3 --> 2 --> 1 --> 0 (--> tail) (左边为头结点,元素存的是下标) */ class Solution { public int[] maxSlidingWindow(int[] nums, int k) { @@ -281,7 +281,7 @@ class Solution { while(!deque.isEmpty() && deque.peek() < i - k + 1){ deque.poll(); } - // 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出 + // 2.维护单调递减队列:新元素若大于队尾元素,则弹出队尾元素,直到满足单调性 while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { deque.pollLast(); } @@ -894,3 +894,4 @@ public: +