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,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.

Show Tags

问题分析:去掉重复项,这里的重复是指3个以上,去重是指将3个以上项降为2个,还是使用传入的数组作为目标数组存储去重后结果,同时返回去重后的数组内数据个数

public class Solution {

public int removeDuplicates(int[] nums) {

int count=0;

int midc=0;

int size=nums.length;

if(size<=1)

return size;

int i;

for(i=0;i<nums.length-1;i++)

{

if(nums[i]!=nums[i+1])

{

midc++;

if(midc<2)

{

nums[count]=nums[i];

count=count+1;

}

else

{

nums[count]=nums[i];

nums[count+1]=nums[i];

count=count+2;

}

midc=0;

}

else

{

midc++;

}

}

if(midc==0)

{

nums[count]=nums[i];

count++;

}

else

{

nums[count]=nums[i];

nums[count+1]=nums[i];

count=count+2;

}

return count;

}

}

时间: 2024-10-06 02:00:05

Leetcode#80Remove Duplicates from Sorted Array II的相关文章

[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 从已排序的数组中删除重复元素

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

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

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I 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 memor