Remove Duplicates from Sorted Array II
来源:互联网 发布:淘宝客优惠券推广 编辑:程序博客网 时间:2024/06/05 18:30
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],
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.
方法与题目: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];//计数清0index=0;}}return i+1;}
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [LeetCode] Longest Valid Parentheses
- POJ3067 Japan【树状数组】【逆序数】
- Java基础——集合框架工具类(Collections类和Arrays类)
- [Java IO]整理
- 为什么不会编程
- Remove Duplicates from Sorted Array II
- shell脚本里切换用户执行命令的注意事项
- 黑马程序员————多线程
- 最大限制相邻选数和
- [C语言][数据结构]基础的线性表的顺序表示和实现
- 例题6-3 矩阵连乘(栈)UVa 442
- 检测机器的CPU的字节序(Big Endian OR Little Endian)
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- webService 路径