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.

 /*************************************************************************
> 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 < )
{
return numsSize;
} int count = ;
int j = ;
int i; for( i=; i<numsSize; i++ )
{
if( nums[i] == nums[i-] )
{
count ++;
if( count < )
{
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 = ;
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=; i<numsSize; i++ )
{
printf("%d ", nums[i]);
}
printf("\n");
} int main()
{
int nums[] = { , , , , , , , };
int numsSize = ; int ret = removeDuplicates( nums, numsSize );
printf("%d\n", ret);
}

LeetCode 80的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. LeetCode 80. 删除排序数组中的重复项 II

    LeetCode 80. 删除排序数组中的重复项 II

  3. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. LeetCode 80,不使用外部空间的情况下对有序数组去重

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第49篇文章,我们一起来看LeetCode的第80题,有序数组去重II(Remove Duplicates fr ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)

    题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. Java实现 LeetCode 80 删除排序数组中的重复项 II(二)

    80. 删除排序数组中的重复项 II 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O ...

  10. Leetcode#80 Remove Duplicates from Sorted Array II

    原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...

随机推荐

  1. Oracle导入导出之dmp

    Oracle导入导出有两种方式,分别是imp/exp与impdp/expdp. 1.imp/exp exp scott/tiger file=d:\test.dmp log=d:\test.log o ...

  2. HDU 5842 Lweb and String (水题)

    Lweb and String 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...

  3. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. 提高Scrum站会效率的一个小工具

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:提高Scrum站会效率的一个小工具.

  5. 剑指OFFER之用两个栈实现队列(九度OJ1512)

    题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作.队列中的元素为int类型. 输入: 每个输入文件包含一个测试样例.对于每个测试样例,第一行输入一个n(1<=n<=100 ...

  6. 糟糕的双重检查加锁(DCL)

    在Java并发编程时,同步都会存在着巨大的性能开销,因此,人们使用了很多的技巧来降低同步的影响,这其中有一些技巧很好,但是也有一些技巧存在一些缺陷,下面要结束的双重检查加锁(DCL)就是有缺陷的一类. ...

  7. UVA12219

    //by Rujia Liu /* 字符串的对比是缓慢的. 鉴于这道题最多只有四个小写字母, 也就是最多26*4种情况, 我们完全可以用整数来代替字符串. 一种比较简单的做法是把字符串看成一个四位的2 ...

  8. 大一下C#五子棋大作业

    上学期的作业,从0开始,到会写C#界面,再到设计出AI对战,跟队友一起用了半个学期的时间,现在才过了几个月就感觉有些遗忘了,赶紧来总结一下. 先上文件吧:程序+源代码 编译环境VS2013 百度云的分 ...

  9. cocos2d-x的初步学习二十一之iosandroid跨平台环境配置

    这篇文章中,我们将来构建下跨平台开发的环境配置,我自己也是参考了别人了文章,折腾了几个小时,尤其是android的配置相对麻烦些.... 参考自子龙山人:http://www.cnblogs.com/ ...

  10. 用shader使图片背景透明

    转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个 ...