LeetCode 80
2016-05-20 12:48
357 查看
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.
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.
/************************************************************************* > File Name: LeetCode080.c > Author: Juntaran > Mail: JuntaranMail@gmail.com > Created Time: Tue 17 May 2016 20:54:02 PM CST ************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> int removeDuplicates(int* nums, int numsSize) { if(numsSize < 2) { return numsSize; } int count = 1; int j = 1; int i; for( i=1; i<numsSize; i++ ) { if( nums[i] == nums[i-1] ) { count ++; if( count < 3 ) { nums[j++] = nums[i]; } else { printf("***\n"); } printf("i=%d j=%d nums[i]=%d nums[j]=%d\n", i,j,nums[i],nums[j]); printfNums(nums,numsSize); } else { nums[j++] = nums[i]; count = 1; printf("i=%d j=%d nums[i]=%d nums[j]=%d\n", i,j,nums[i],nums[j]); printfNums(nums,numsSize); } } return j; } void printfNums( int* nums, int numsSize ) { int i; for( i=0; i<numsSize; i++ ) { printf("%d ", nums[i]); } printf("\n"); } int main() { int nums[] = { 1, 2, 3, 3, 3, 5, 5, 5 }; int numsSize = 8; int ret = removeDuplicates( nums, numsSize ); printf("%d\n", ret); }
相关文章推荐
- 结构体大小的计算,对齐
- Swift 元组
- JAXB - Annotations, Top-level Elements: XmlRootElement
- LeetCode 75
- LeetCode 61
- triangle- 求从顶向下的三角形的最小路径和问题
- Android Monkey的用法
- LeetCode 45
- OneASP 安全公开课,深圳站, Come Here, Feel Safe!
- Spring 源码解析之HandlerAdapter源码解析(二)
- LeetCode 26
- LeetCode 19
- GDAL栅格矢量化
- http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml
- 使用mapreduce计算环比的实例
- OneASP 安全公开课,深圳站, Come Here, Feel Safe!
- LeetCode 3
- springmvc与struts2的区别
- nginx开启PHP错误日志
- jQueryMobile: selectmenu("refresh", true)