序言:第一次刷 LeetCode 的思维觉醒 作为一名习惯了 JavaScript 灵活语法的开发者,第一次踏入算法的世界,我逐渐意识到,算法练习并不是在学习如何使用 API,而是在学习如何精准地操控内存。 在这个系列中,我将记录下我攻克每一种数据结构的历程。起点便从最常见、操作也最为细腻的数组开始。
数组篇:原地算法(In-place)的逻辑美学 在原地算法中,我们必须像操作底层语言那样去对待 JS 数组:
拒绝“黑盒”操作:弃用 splice()。虽然它能一键删除,但会引发后续元素的集体位移,导致时间复杂度飙升至 O(n2)O(n^2)O(n2)。 拥抱覆盖(Overwrite) :原地算法的真谛不是“删除”,而是用“有效”的数据去覆盖那些“不需要”的数据,将空间复杂度压制在 O(1)O(1)O(1)。
经典题型一:合并两个有序数组 (LeetCode 88)
- 题目描述 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n。请你合并 nums2 到 nums1 中,使合并后的数组同样按非递减顺序排列。
注意:nums1 初始化长度为 m + n,其中前 m 个元素表示有效数据,后 n 个元素为 0。
核心思路:逆向双指针 如果从前往后合并,每次向 nums1 插入数据都要挪动其后的所有元素。 巧妙之处:利用 nums1 后端预留的“0”空位,从后往前比较。由于是从最大值开始向最大的索引位填补,我们永远不会在处理完 nums1 的有效数字前覆盖掉它们。
代码实现 JavaScript ini 体验AI代码助手 代码解读复制代码var merge = function(nums1, m, nums2, n) { let p1 = m - 1; // nums1 有效数据末尾 let p2 = n - 1; // nums2 末尾 let p = m + n - 1; // nums1 物理末尾
while (p1 >= 0 && p2 >= 0) {
// 谁大谁往后面放,填补后端空位 if (nums1[p1] > nums2[p2]) { nums1[p--] = nums1[p1--]; } else { nums1[p--] = nums2[p2--]; }} // 特殊情况:如果 nums2 还没搬完(nums2 还有更小的数),补齐到前端 while (p2 >= 0) {
nums1[p--] = nums2[p2--];} };
经典题型二:移除指定元素 (LeetCode 27)
- 题目描述 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素。返回移除后数组的新长度 k。你不需要考虑数组中超出新长度后面的元素。
- 核心思路:快慢指针(读写分离) 这道题教会了我“逻辑删除”。
快指针 (fast) :充当“探路者”,寻找数组中不等于 val 的有效元素。
慢指针 (slow) :充当“记录员”,锁定下一个可以被写入的坑位。 本质:这是一场“洗牌”,快指针负责把好的牌递给慢指针,慢指针按顺序在前端排好。
- 代码实现
JavaScript
ini 体验AI代码助手 代码解读复制代码var removeElement = function(nums, val) {
let slow = 0;
for (let fast = 0; fast < nums.length; fast++) {
} return slow; // 返回有效区域的长度 };// 只有当快指针发现有效数据时,才写入慢指针指向的“安全区” if (nums[fast] !== val) { nums[slow] = nums[fast]; slow++; }
经典题型三:删除有序数组中的重复项 (LeetCode 26)
- 题目描述 给你一个 非严格递增排列 的数组 nums,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的相对顺序应该保持一致。
- 核心思路:保送机制与前项对比 既然数组是有序的,那么重复的元素必然是相邻的。
逻辑:我们只需要比较当前快指针看到的数,是否和慢指针区域中“最后一个确定的数”相同。
避坑细节:这是我第一次遇到索引越界问题。
技巧:将 slow 和 fast 都从 1 开始。因为第一个元素 nums[0] 前面没有数字,它绝对不可能是重复项,所以它被“保送”通过。 安全:通过比较 nums[fast] 与 nums[slow - 1],我们永远不会触碰到 nums[-1]。
代码实现 JavaScript ini 体验AI代码助手 代码解读复制代码var removeDuplicates = function(nums) { if (nums.length === 0) return 0;
let slow = 1; // 第一个元素默认有效,直接跳过处理 for (let fast = 1; fast < nums.length; fast++) {
// 拿当前探路数与已确定的前一个有效数对比 // 如果不同,说明遇到了一个新的唯一数字 if (nums[fast] !== nums[slow - 1]) { nums[slow] = nums[fast]; slow++; }} return slow; };
北京 成都
海口
https://infogram.com/9862pdf-1hnq41opz805p23
https://infogram.com/9862pdf-1hxj48mqegpv52v
https://infogram.com/9862pdf-1h0r6rzw5yp8w4e
https://infogram.com/9862pdf-1hmr6g8jzx7lz2n
https://infogram.com/9862pdf-1h9j6q75k9go54g
https://infogram.com/9862pdf-1hnq41opz80ek23
https://infogram.com/9862pdf-1hnp27eqydmdn4g
https://infogram.com/9862pdf-1hxj48mqegpeq2v
https://infogram.com/9862pdf-1h0n25oplqo8l4p
https://infogram.com/9862pdf-1h1749wqmgz1q2z
https://infogram.com/9862pdf-1hnp27eqyrd9y4g
https://infogram.com/9862pdf-1h984wv1drd3z2p
https://infogram.com/9862pdf-1hmr6g8jzrz8z2n
https://infogram.com/9862pdf-1h9j6q75kekj54g
https://infogram.com/9862pdf-1h7v4pd08y8x84k
https://infogram.com/9862pdf-1hxj48mqedewq2v
https://infogram.com/9862pdf-1hmr6g8jzrzno2n
https://infogram.com/9862pdf-1h1749wqmgmgq2z
https://infogram.com/9862pdf-1h984wv1drkoz2p
https://infogram.com/9862pdf-1h0n25oplv1zz4p
https://infogram.com/9862pdf-1h9j6q75kelx54g
https://infogram.com/9862pdf-1hnq41opzer1k23
https://infogram.com/9862pdf-1hxj48mqedlqq2v
https://infogram.com/9862pdf-1hmr6g8jzrvro2n
https://infogram.com/9862pdf-1h0n25oplv1ll4p
https://infogram.com/9862pdf-1h9j6q75kelnv4g
https://infogram.com/9862pdf-1hnq41opzervp23
https://infogram.com/9862pdf-1hnp27eqyr7wy4g
https://infogram.com/9862pdf-1h984wv1drmvz2p
https://infogram.com/9862pdf-1hmr6g8jzr0wz2n
https://infogram.com/9862pdf-1h0n25oplvndz4p
https://infogram.com/9862pdf-1hxj48mqed90q2v
https://infogram.com/9862pdf-1h984wv1drmgd2p
https://infogram.com/9862pdf-1h0r6rzw5m7ml4e
https://infogram.com/9862pdf-1h1749wqmgymq2z
https://infogram.com/9862pdf-1h9j6q75kenzv4g
https://infogram.com/9862pdf-1hnq41opzed7p23
https://infogram.com/9862pdf-1hnp27eqyr93y4g
https://infogram.com/9862pdf-1h984wv1dryjz2p
https://infogram.com/9862pdf-1hmr6g8jzr51z2n
https://infogram.com/9862pdf-1h1749wqmgenl2z
https://infogram.com/9862pdf-1h9j6q75kez854g
https://infogram.com/9862pdf-1h7v4pd08ygk84k
https://infogram.com/9862pdf-1hxj48mqedyjq2v
https://infogram.com/9862pdf-1h984wv1dry7d2p
https://infogram.com/9862pdf-1h0n25oplv7nl4p
https://infogram.com/9862pdf-1hnq41opzevkp23
https://infogram.com/9862pdf-1hxj48mqedk352v
https://infogram.com/9862pdf-1h0r6rzw5mn5l4e
https://infogram.com/9862pdf-1h1749wqmg5yq2z
https://infogram.com/9862pdf-1h9j6q75ke13v4g
https://infogram.com/9862pdf-1hnq41opze70p23
https://infogram.com/9862pdf-1h0r6rzw5mjvw4e
https://infogram.com/9862pdf-1h0n25oplv59z4p
https://infogram.com/9862pdf-1hnq41opze7jk23
https://infogram.com/9862pdf-1hnp27eqyrwgn4g
https://infogram.com/9862pdf-1hxj48mqedxdq2v
https://infogram.com/9862pdf-1h0r6rzw5mj9l4e
https://infogram.com/9862pdf-1h7v4pd08lnwj4k
https://infogram.com/9862pdf-1hnq41opz8kop23
https://infogram.com/9862pdf-1hnp27eqyd3ky4g
https://infogram.com/9862pdf-1hxj48mqeg5o52v
https://infogram.com/9862pdf-1h984wv1d53zz2p
https://infogram.com/9862pdf-1hmr6g8jzxdyz2n
https://infogram.com/9862pdf-1h1749wqmzl8l2z
https://infogram.com/9862pdf-1h7v4pd08lnv84k
https://infogram.com/9862pdf-1hxj48mqeg5gq2v
https://infogram.com/9862pdf-1hmr6g8jzxd5o2n
https://dribbble.com/zhanqiu4/collections/7730381
https://dribbble.com/zhanqiu4/collections/7730382
https://dribbble.com/zhanqiu4/collections/7730383
https://dribbble.com/zhanqiu4/collections/7730384
https://dribbble.com/zhanqiu4/collections/7730385
https://dribbble.com/zhanqiu4/collections/7730386
https://dribbble.com/zhanqiu4/collections/7730387
https://dribbble.com/zhanqiu4/collections/7730388
https://dribbble.com/zhanqiu4/collections/7730389
https://dribbble.com/zhanqiu4/collections/7730390
https://dribbble.com/zhanqiu4/collections/7730394
https://dribbble.com/zhanqiu4/collections/7730395
https://dribbble.com/zhanqiu4/collections/7730393
https://dribbble.com/zhanqiu5/collections/7730415
https://dribbble.com/zhanqiu5/collections/7730414
https://dribbble.com/zhanqiu5/collections/7730413
https://dribbble.com/zhanqiu5/collections/7730412
https://dribbble.com/zhanqiu5/collections/7730411
https://dribbble.com/zhanqiu5/collections/7730409
https://dribbble.com/zhanqiu5/collections/7730408
https://dribbble.com/zhanqiu5/collections/7730406
https://dribbble.com/zhanqiu5/collections/7730405
https://dribbble.com/zhanqiu5/collections/7730404
https://dribbble.com/zhanqiu5/collections/7730403
https://dribbble.com/zhanqiu5/collections/7730402
https://dribbble.com/zhanqiu5/collections/7730401
https://dribbble.com/zhanqiu5/collections/7730400
https://infogram.com/9862pdf-1h9j6q75k955v4g
https://infogram.com/9862pdf-1h0n25oplqp5z4p
https://infogram.com/9862pdf-1h7v4pd08lvw84k
https://infogram.com/9862pdf-1hnp27eqyd05n4g
https://infogram.com/9862pdf-1h984wv1d5wzd2p
https://infogram.com/9862pdf-1h0r6rzw5yo8l4e
https://infogram.com/9862pdf-1h1749wqmz08q2z
https://infogram.com/9862pdf-1hnq41opz8ymp23
https://infogram.com/9862pdf-1h1749wqmz0pl2z
https://infogram.com/9862pdf-1h7v4pd08lqd84k
https://infogram.com/9862pdf-1hnp27eqdjm7n4g
https://infogram.com/9862pdf-1h0r6rzwyqpjl4e
https://infogram.com/9862pdf-1h0n25opqjool4p
https://infogram.com/9862pdf-1hxj48mqgjmn52v
https://infogram.com/9862pdf-1h1749wqzjwjl2z
https://infogram.com/9862pdf-1h9j6q759q7e54g
https://infogram.com/9862pdf-1h984wv15gved2p
https://infogram.com/9862pdf-1h0n25opqjkkl4p
https://infogram.com/9862pdf-1h9j6q759q0pv4g
https://infogram.com/9862pdf-1hnp27eqdjkxy4g
https://infogram.com/9862pdf-1h0n25opqj80z4p
https://infogram.com/9862pdf-1h7v4pd0lqn584k
https://infogram.com/9862pdf-1hnp27eqdj3yn4g
https://infogram.com/9862pdf-1h984wv15g3md2p
https://infogram.com/9862pdf-1h1749wqzjvlq2z
https://infogram.com/9862pdf-1h0n25opqjyyl4p
https://infogram.com/9862pdf-1h7v4pd0lqw9j4k
https://infogram.com/9862pdf-1hxj48mqgjpr52v
https://infogram.com/9862pdf-1h0r6rzwyqpkw4e
https://infogram.com/9862pdf-1h1749wqzjv0l2z
https://infogram.com/untitled-1h0n25opq07el4p
https://infogram.com/untitled-1hnq41op8yvop23
https://infogram.com/untitled-1hnp27eqd08ky4g
https://infogram.com/untitled-1h1749wqz058l2z
https://infogram.com/untitled-1h1749wqz058l2z
https://infogram.com/9862pdf-1h7v4pd0lvoq84k
https://infogram.com/9862pdf-1h984wv15w9kd2p
https://infogram.com/9862pdf-1h9j6q759qv7v4g
https://infogram.com/untitled-1hnq41op8jk9p23
https://infogram.com/9862pdf-1hxj48mqgj5152v
https://infogram.com/untitled-1h0r6rzwyo93w4e
https://infogram.com/untitled-1h9j6q759ol1v4g
https://infogram.com/untitled-1h0r6rzwyo7ew4e
https://infogram.com/microsoft-office-word-2007-docx-1h7v4pd0lv1r84k
https://infogram.com/untitled-1hxj48mqg09zq2v
https://infogram.com/untitled-1h9j6q759on3v4g
https://infogram.com/untitled-1h984wv15wyqz2p
https://infogram.com/untitled-1h0r6rzwyo1vw4e



