[leetcode]_Remove Duplicates from Sorted Array II

题目:一个有序数组,要求保证数组中的每个元素不能超过2个。

    输入:A = [1,1,1,2,2,3]  输出:length
5, and A is
now [1,1,2,2,3]

思路:双指针
。有些绕,不过理清了后,思路还是很直接明朗的。

1、两个指针:p和help。初始化时同时指向数组头。变量cur记录当前元素值,count记录当前元素值出现的次数。

2、当 A[p] == cur && count < 2 或者 A[p] != cur 的时候,A[help] = A[p],
help++ , p++。

3、当 A[p] == cur && count >= 2时,p++,help不动。

4、直到p指向尾部,此时help的值即为去重后的数组长度。

代码:


 1 public int removeDuplicates(int[] A) {
2 if(A.length == 0) return 0;
3
4 int cur = A[0] , count = 1;
5 int help = 1;
6 for(int p = 1 ; p < A.length ; p++){
7 if(cur == A[p]){
8 if(count < 2){
9 count++;
10 A[help++] = A[p];
11 }
12 }else{
13 count = 1;
14 cur = A[p];
15 A[help++] = A[p];
16 }
17 }
18 return help;
19 }

时间: 2025-01-01 02:00:29

[leetcode]_Remove Duplicates from Sorted Array II的相关文章

leetcode第一刷_Remove Duplicates from Sorted Array II

水题. 我之前说过包含至多几个至少几个的问题都比较难,这个题可是让我大脸了.至多可以重复一次,那就重复次数多于两次再计算重复,否则的话像普通的数据一样直接按照重复次数前移就可以了嘛.不过说归说,这种inspace的思想还是有些用处的,数组这种实现方式致命的缺点就是删除或者添加中间的元素代价太大,因为不好把握数据的最终位置.这个题是一种情况,合并两个排序好的数组也是一个例子. class Solution { public: int removeDuplicates(int A[], int n)

[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#80Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II Total Accepted: 39950 Total Submissions: 130101My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2

[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 sorte

[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() Remove Duplicates from Sorted Array II

我的思路: 先判断是不是有两个相等的,如果有,从2个之后查找还有几个相同的,start计数. class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size() == 0) return 0; int like=nums[0]; int flag=0; int start=0; for(int i=1;i<nums.size();++i) { if(like==nums[i]) { i