[Leetcode] 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类似的思想。隔一个比较一次,拿A =[1,1,1,2,2,3]举例子,其对比的过程如下

这里可以将第6行、第10行的2改为3,则至多重复的数字出现三次,代码为:

 1 class Solution
 2 {
 3 public:
 4     int removeDuplicates(int A[], int n)
 5     {
 6         if(n<=2)    return n;
 7         int lo=2;
 8         for(int i=2;i<n;++i)
 9         {
10             if(A[i] !=A[lo-2])
11                 A[lo++]=A[i];
12         }
13         return index;
14     }
15 };

方法二:使用计数器,当两者不相等计数器重置,将A[i]赋值给A[lo]的下一位;相等时但是count不超过2,将A[i]赋值给A[lo]的下一位,其余,仅计数器++,这样就可以成功将不等的情况后的,出现第二个和A[i]相等的情况时,将其也赋值给前面的元素。

 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n)
 4     {
 5         int lo=0;
 6         int count=0;
 7         if(n<3) return n;
 8
 9         for(int i=1;i<n;i++)
10         {
11             if(A[lo]==A[i])
12             {
13                 count++;
14                 if(count<2)
15                     A[++lo]=A[i];
16             }
17             else
18             {
19                 A[++lo]=A[i];
20                 count=0;
21             }
22         }
23         return lo+1;
24     }
25 };
时间: 2024-08-02 11:00:38

[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素的相关文章

[Leetcode] Remove duplicate 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,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3. 这题和R

leetcode 题解: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]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

[Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A =[1

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

题目 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]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] 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 fro

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: 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

[Leetcode] Remove Duplicates From Sorted Array II (C++)

题目: 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]. Tag: Array; Two Pointers 体会: 继续是quicksort中par

[LeetCode] 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