[LeetCode][JavaScript]Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn‘t matter what you leave beyond the new length.

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/



删除数组中重复出现三次或三次以上的数。

两个变量记录上次和上上的值,比较一下,如果出现大于等于三次了,记下下标。

删除的时候要从后往前,这样不会打乱下标的顺序。

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5 var removeDuplicates = function(nums) {
 6     var aRemove = [], i, previous, previous2;
 7     for(i = 0; i < nums.length; i++){
 8         if(nums[i] !== previous){
 9             //first time
10         }else if(nums[i] === previous && nums[i] !== previous2){
11             //duplicate two times
12         }else{
13             //duplicate more than two times
14             aRemove.push(i);
15         }
16         previous2 = previous;
17         previous = nums[i];
18     }
19     for(i = aRemove.length - 1; i >= 0; i--){
20         nums.splice(aRemove[i], 1);
21     }
22     return nums.length;
23 };

另一种更简洁的做法,开一个变量index,每次把正确的结果放到下标为index的位置上,index++,遍历完之后index就是目标数组的长度。

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5 var removeDuplicates = function(nums) {
 6     var index = 0, i, previous, previous2;
 7     for(i = 0; i < nums.length; i++){
 8         if(nums[i] !== previous){
 9             nums[index++] = nums[i];
10         }else if(nums[i] === previous && nums[i] !== previous2){
11             nums[index++] = nums[i];
12         }
13         previous2 = previous;
14         previous = nums[i];
15     }
16     return index;
17 };
时间: 2024-10-26 22:35:43

[LeetCode][JavaScript]Remove Duplicates from Sorted Array II的相关文章

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

[LeetCode][JavaScript]Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->

LeetCode OJ Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

[C++]LeetCode: 72 Remove Duplicates from Sorted Array II

题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 思路: 可以借鉴Remove Duplicates from Sorted Array

LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

leetcode 【 Remove Duplicates from Sorted Array II 】python 实现

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 代码:oj测试通过 Runtime: 120 ms 1 class Solution: 2

leetcode 80 Remove Duplicates from Sorted Array II ----- java

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

Java for LeetCode 080 Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'