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 matter what you leave beyond the new length.

此题要求数组中每个元素出现的个数不大于两次。所以需要加一个计数器来统计每个元素出现的次数。

代码如下:

	int removeDuplicates2(vector<int>& nums) {
		int length = nums.size();
		if (length == 0)
			return 0;
		int j=0;
		int times = 1;
		for (int i=1; i<length; i++)
		{
			if (nums[i] != nums[j])
			{
				nums[++j] = nums[i];
				times = 1;
			}else{
				times++;
				if (times == 2)
				{
					nums[++j] = nums[i];
				}
			}
		}
		return j+1;
	}
时间: 2024-09-27 22:34:01

LeetCode 80:Remove Duplicates from Sorted Array II的相关文章

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 II

一.题目描述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是类似的,只不过这里允许出现重复的数字而已,可以采用二分搜索的变种算法,只不过加入了剔除和第一个元素相同的元素的过程.另一个思路是加入一个变量,用于记录元素出现的次数.这题因为是已经排序的数组,所以一个变量即可解决.如果是没有排序的数组,则需要引入一个hash表来记录出现次数. 三.示例代码 class Solution { public: int RemoveDuplicatesFr

leetcode || 80、Remove Duplicates from Sorted Array II

problem: 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]. Hide Tags Array Two Pointers 题意:对数组进行去

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

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

LeetCode OJ:Remove Duplicates from Sorted Array(排好序的vector去重)

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 nums 

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 n

leetcode笔记:Remove Duplicates from Sorted Array

一.题目描述 二.解题技巧 从题目中可知,数组中的元素事先已经过排序,因此一个简单而易于实现的方法是从第二个元素开始对数组元素进行遍历,并判断该元素是否和前面的元素相同.题目要求返回不重复的元素的个数. 算法的一个要求是:返回的数组的元素都是不重复,同时要求remove duplicates in place,这意味着不能重新定义一个数组来保存不重复的元素.假设该数组为A,为了满足这一要求,可以设置一个变量num来保存不重复的元素的个数,然后遍历整个数组A,如果该元素A[i]!=A[i-1]的话

LeetCode OJ:Remove Duplicates from Sorted List II(链表去重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->1->1->2->3, return 2->3.