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.
方法与题目:Remove Duplicates from Sorted Array类似,同样是移除重复元素,但是允许元素最多重复两次,用一个变量记录重复次数,当不重复
时,变量清0,AC代码如下:
int removeDuplicates(int* nums, int numsSize) { //定义两个游标 int i,j; //记数 int index=0; if (numsSize==0) return 0; i=0; for (j=1; j<numsSize; j++) { if (nums[i]==nums[j]) { index++; if (index<2) { nums[++i]=nums[j]; } } else { nums[++i]=nums[j]; //计数清0 index=0; } } return i+1; }
Remove Duplicates from Sorted Array II
原文地址:http://blog.csdn.net/lsh_2013/article/details/45700773