leetcode-Remove Duplicates from Sorted Array II-80

输入非递减数组,要求每个元素最多重复两次,求最后剩下的数组和元素个数

这题函数的返回值是元素个数,但是还需要把输入的参数也就是输入的数组也改动成合法的

因为是有序的序列,所以直接遍历一遍,用cur保存元素,cnt保存这个元素出现的次数就好,ON

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         int ans=0;
 5         if(nums.size()==0) return ans;
 6         int cur=nums[0];
 7         int cnt=1;
 8         ans=1;
 9         for(int i=1;i<nums.size();i++){
10             if(nums[i]==cur){
11                 cnt++;
12                 if(cnt<=2) nums[ans++]=nums[i];
13             }
14             else{
15                 cur=nums[i];
16                 cnt=1;
17                 nums[ans++]=nums[i];
18             }
19         }
20         return ans;
21     }
22 };
时间: 2024-10-11 21:38:11

leetcode-Remove Duplicates from Sorted Array II-80的相关文章

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

两个题类似,第一个题是不允许有重复的数字,第二个题是允许每个数字最多重复两个,两个题目都要求在原数组上进行操作,并返回生成数组的长度,即空间复杂度为O(1). 两个题都是使用双指针,第一个指针指向生成新的数组的最后一个位置,第二个指针指向当前进行判断的位置. 唯一不同的是, 第二个题需要设置一个变量来控制重复的个数是否超过2 26. Remove Duplicates from Sorted Array class Solution { public: int removeDuplicates(

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