From 77789ffe7e38e0e69a1c11b7bb5a0e763a8c5ade Mon Sep 17 00:00:00 2001 From: labuladong Date: Fri, 21 Jun 2024 20:16:16 +0800 Subject: [PATCH] update content --- README.md | 8 ++--- 算法思维系列/双指针技巧.md | 2 +- 算法思维系列/集合划分.md | 14 ++++++++ 高频面试系列/判断回文链表.md | 45 ++++++++++++++++-------- 高频面试系列/子集排列组合.md | 4 ++- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c32a9fa..e88f299 100644 --- a/README.md +++ b/README.md @@ -172,14 +172,14 @@ PDF 共两本,一本《labuladong 的算法秘籍》类似教材,帮你系 * [我的刷题心得:算法的本质](https://labuladong.online/algo/fname.html?fname=算法心得) * [双指针技巧秒杀七道链表题目](https://labuladong.online/algo/fname.html?fname=链表技巧) * [双指针技巧秒杀七道数组题目](https://labuladong.online/algo/fname.html?fname=双指针技巧) + * [我写了首诗,把滑动窗口算法变成了默写题](https://labuladong.online/algo/fname.html?fname=滑动窗口技巧进阶) + * [我写了首诗,把二分搜索算法变成了默写题](https://labuladong.online/algo/fname.html?fname=二分查找详解) * [东哥带你刷二叉树(纲领篇)](https://labuladong.online/algo/fname.html?fname=二叉树总结) * [动态规划解题套路框架](https://labuladong.online/algo/fname.html?fname=动态规划详解进阶) * [回溯算法解题套路框架](https://labuladong.online/algo/fname.html?fname=回溯算法详解修订版) * [回溯算法秒杀所有排列/组合/子集问题](https://labuladong.online/algo/fname.html?fname=子集排列组合) * [球盒模型:回溯算法穷举的两种视角](https://labuladong.online/algo/fname.html?fname=回溯两种视角) * [BFS 算法解题套路框架](https://labuladong.online/algo/fname.html?fname=BFS框架) - * [我写了首诗,把滑动窗口算法变成了默写题](https://labuladong.online/algo/fname.html?fname=滑动窗口技巧进阶) - * [我写了首诗,把二分搜索算法变成了默写题](https://labuladong.online/algo/fname.html?fname=二分查找详解) * [算法时空复杂度分析实用指南](https://labuladong.online/algo/fname.html?fname=时间复杂度) @@ -254,10 +254,10 @@ PDF 共两本,一本《labuladong 的算法秘籍》类似教材,帮你系 * [算法就像搭乐高:带你手撸 LRU 算法](https://labuladong.online/algo/fname.html?fname=LRU算法) * [算法就像搭乐高:带你手撸 LFU 算法](https://labuladong.online/algo/fname.html?fname=LFU) * [【强化练习】哈希表更多习题](https://labuladong.online/algo/fname.html?fname=哈希表习题) - * [前缀树算法模板秒杀五道算法题](https://labuladong.online/algo/fname.html?fname=trie) - * [一道求中位数的算法题把我整不会了](https://labuladong.online/algo/fname.html?fname=数据流中位数) * [二叉堆详解实现优先级队列](https://labuladong.online/algo/fname.html?fname=二叉堆实现) * [【强化练习】优先级队列经典习题](https://labuladong.online/algo/fname.html?fname=二叉堆习题) + * [一道求中位数的算法题把我整不会了](https://labuladong.online/algo/fname.html?fname=数据流中位数) + * [前缀树算法模板秒杀五道算法题](https://labuladong.online/algo/fname.html?fname=trie) * [设计朋友圈时间线功能](https://labuladong.online/algo/fname.html?fname=设计Twitter) * [【强化练习】更多经典设计习题](https://labuladong.online/algo/fname.html?fname=设计习题) diff --git a/算法思维系列/双指针技巧.md b/算法思维系列/双指针技巧.md index 933e2eb..94b8f5b 100644 --- a/算法思维系列/双指针技巧.md +++ b/算法思维系列/双指针技巧.md @@ -214,7 +214,7 @@ int removeElement(int[] nums, int val); ```cpp /* 滑动窗口算法框架 */ -void slidingWindow(string s, string t) { +void slidingWindow(string s) { unordered_map window; int left = 0, right = 0; diff --git a/算法思维系列/集合划分.md b/算法思维系列/集合划分.md index e59fc27..eb6b370 100644 --- a/算法思维系列/集合划分.md +++ b/算法思维系列/集合划分.md @@ -550,6 +550,20 @@ class Solution { +
+
+引用本文的题目 + +安装 [我的 Chrome 刷题插件](https://labuladong.online/algo/intro/chrome/) 点开下列题目可直接查看解题思路: + +| LeetCode | 力扣 | +| :----: | :----: | +| [473. Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/?show=1) | [473. 火柴拼正方形](https://leetcode.cn/problems/matchsticks-to-square/?show=1) | + +
+
+ + **_____________** diff --git a/高频面试系列/判断回文链表.md b/高频面试系列/判断回文链表.md index 0db5bc8..a347bc9 100644 --- a/高频面试系列/判断回文链表.md +++ b/高频面试系列/判断回文链表.md @@ -133,27 +133,42 @@ void traverse(ListNode head) { ```java -// 左侧指针 -ListNode left; +class Solution { + // 从左向右移动的指针 + ListNode left; + // 从右向左移动的指针 + ListNode right; -boolean isPalindrome(ListNode head) { - left = head; - return traverse(head); -} + // 记录链表是否为回文 + boolean res = true; -boolean traverse(ListNode right) { - if (right == null) return true; - boolean res = traverse(right.next); - // 后序遍历代码 - res = res && (right.val == left.val); - left = left.next; - return res; + boolean isPalindrome(ListNode head) { + left = head; + traverse(head); + return res; + } + + void traverse(ListNode right) { + if (right == null) { + return; + } + + // 利用递归,走到链表尾部 + traverse(right.next); + + // 后序遍历位置,此时的 right 指针指向链表右侧尾部 + // 所以可以和 left 指针比较,判断是否是回文链表 + if (left.val != right.val) { + res = false; + } + left = left.next; + } } ``` -这么做的核心逻辑是什么呢?**实际上就是把链表节点放入一个栈,然后再拿出来,这时候元素顺序就是反的**,只不过我们利用的是递归函数的堆栈而已,如下 GIF 所示: +这么做的核心逻辑是什么呢?**实际上就是把链表节点放入一个栈,然后再拿出来,这时候元素顺序就是反的**,只不过我们利用的是递归函数的堆栈而已。如果不好理解,可以看下面这个可视化面板,你可以不断点击 `traverse(right.next);` 这一行代码,就能看出 `left, right` 的移动过程了: -![](https://labuladong.online/algo/images/回文链表/1.gif) + 当然,无论造一条反转链表还是利用后序遍历,算法的时间和空间复杂度都是 O(N)。下面我们想想,能不能不用额外的空间,解决这个问题呢? diff --git a/高频面试系列/子集排列组合.md b/高频面试系列/子集排列组合.md index 377aff5..3bb3ec8 100644 --- a/高频面试系列/子集排列组合.md +++ b/高频面试系列/子集排列组合.md @@ -817,7 +817,7 @@ class Solution { ### 排列(元素无重可复选) -力扣上没有类似的题目,我们不妨先想一下,`nums` 数组中的元素无重复且可复选的情况下,会有哪些排列? +力扣上没有题目直接考察这个场景,我们不妨先想一下,`nums` 数组中的元素无重复且可复选的情况下,会有哪些排列? 比如输入 `nums = [1,2,3]`,那么这种条件下的全排列共有 3^3 = 27 种: @@ -1027,6 +1027,8 @@ void backtrack(int[] nums) { | [368. Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/?show=1) | [368. 最大整除子集](https://leetcode.cn/problems/largest-divisible-subset/?show=1) | | [491. Non-decreasing Subsequences](https://leetcode.com/problems/non-decreasing-subsequences/?show=1) | [491. 递增子序列](https://leetcode.cn/problems/non-decreasing-subsequences/?show=1) | | [638. Shopping Offers](https://leetcode.com/problems/shopping-offers/?show=1) | [638. 大礼包](https://leetcode.cn/problems/shopping-offers/?show=1) | +| [967. Numbers With Same Consecutive Differences](https://leetcode.com/problems/numbers-with-same-consecutive-differences/?show=1) | [967. 连续差相同的数字](https://leetcode.cn/problems/numbers-with-same-consecutive-differences/?show=1) | +| [996. Number of Squareful Arrays](https://leetcode.com/problems/number-of-squareful-arrays/?show=1) | [996. 正方形数组的数目](https://leetcode.cn/problems/number-of-squareful-arrays/?show=1) | | - | [剑指 Offer 38. 字符串的排列](https://leetcode.cn/problems/zi-fu-chuan-de-pai-lie-lcof/?show=1) | | - | [剑指 Offer II 079. 所有子集](https://leetcode.cn/problems/TVdhkn/?show=1) | | - | [剑指 Offer II 080. 含有 k 个元素的组合](https://leetcode.cn/problems/uUsW3B/?show=1) |