[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 1122 and 3. It doesn't matter what you leave beyond the new length.

【分析】和上篇博文类似,但是这里容许两个重复,所以判断条件修改一下。

http://blog.csdn.net/xy010902100449/article/details/49103793

代码如下:

int removeDuplicates(int* nums, int numsSize) 
{
    if(!nums||numsSize<=0) {
		return 0;
	}
	int i = 0;
	int count = 0;

	for(i = 0 ;i<numsSize;i++) {
		while((nums[i]==nums[i+1])&&
			  (nums[i+1]==nums[i+2])&&(i+2)<numsSize) {
			i++;
		}
		nums[count] = nums[i];
		count++;
	}

	return count;
}


你可能感兴趣的:(LeetCode)