From d23cc5785a89d98f1deb2d385ad22bd1f75d3281 Mon Sep 17 00:00:00 2001 From: Wen <409053122@qq.com> Date: Fri, 30 Jun 2023 11:14:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:[80]=20=E5=88=A0=E9=99=A4=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E7=9A=84=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E9=A1=B9=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../80.删除有序数组中的重复项-ii.js | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/leecode/javascript/80.删除有序数组中的重复项-ii.js diff --git a/src/leecode/javascript/80.删除有序数组中的重复项-ii.js b/src/leecode/javascript/80.删除有序数组中的重复项-ii.js new file mode 100644 index 0000000..197ea63 --- /dev/null +++ b/src/leecode/javascript/80.删除有序数组中的重复项-ii.js @@ -0,0 +1,50 @@ +/* + * @lc app=leetcode.cn id=80 lang=javascript + * + * [80] 删除有序数组中的重复项 II + */ + +// @lc code=start +/** + * @param {number[]} nums + * @return {number} + */ +var removeDuplicates = function (nums) { + //重复次数 + let repeat = 1; + //当前判定数 + let cur = nums[0]; + //当前移动到的位置 + let index = 1; + //有效尾部 + let tail = nums.length - 1; + for (; index < nums.length; index++) { + if (nums[index] < cur) { + index--; + break; + } + if (nums[index] == cur) { + repeat++; + } else { + repeat = 1; + cur = nums[index]; + } + if (repeat >= 3) { + if (nums[tail] == cur) { + return index--; + } + let cursor = index + 1; + while (cursor <= tail) { + [nums[cursor - 1], nums[cursor]] = [nums[cursor], nums[cursor - 1]]; + cursor++; + } + tail--; + index--; + } + } + return index + 1; +}; + +console.log(removeDuplicates([0, 0, 1, 1, 1, 1, 2, 3, 3])); + +// @lc code=end