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. Java设计模式系列之观察者模式

    观察者模式 Observer的定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象. 这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己. 第一 ...

  2. POJ 1064 Cable master (二分答案)

    题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...

  3. Elasticsearch和mysql数据同步(elasticsearch-jdbc)

    1.介绍 对mysql.oracle等数据库数据进行同步到ES有三种做法:一个是通过elasticsearch提供的API进行增删改查,一个就是通过中间件进行数据全量.增量的数据同步,另一个是通过收集 ...

  4. 下载Xml文件方法

    #region 下载Xml文件方法 //定义委托 private delegate void DownLoadDelegate(string url, string filename); privat ...

  5. CentOS 6.3(x86_64)下安装Oracle 10g R2

    目 录 一.硬件要求二.软件三.系统安装注意四.安装Oracle前的系统准备工作五.安装Oracle,并进行相关设置六.升级Oracle到patchset 10.2.0.4七.使用rlwrap调用sq ...

  6. Weka – 分类

    1.      weka简单介绍 1)  weka是新西兰怀卡托大学WEKA小组用JAVA开发的机器学习/数据挖掘开源软件. 2)  相关资源链接 http://sourceforge.net/pro ...

  7. 脚本命令高级Bash脚本编程指南(31):数学计算命令

    题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...

  8. sql 指令

    SELECT 是用来做什么的呢?一个最经常使用的方式是将资料从数据库中的表格内选出.从这一句回答中.我们立即能够看到两个keyword:从 (FROM)数据库中的表格内选出 (SELECT).(表格是 ...

  9. Java网页数据采集器[上篇-数据采集]【转载】

    开篇 作为全球运用最广泛的语言,Java 凭借它的高效性,可移植性(跨平台),代码的健壮性以及强大的可扩展性,深受广大应用程序开发者的喜爱. 作为一门强大的开发语言,正则表达式在其中的应用当然是必不可 ...

  10. 实现Android半透明Menu效果的开发实例

    不知道大家是否用过天天动听,对于它界面上的半透明Menu效果,笔者感觉非常漂亮.下面是天天动听半透明Menu的截图,欣赏下吧: 感觉还不错吧?那么如何实现这种半透明Menu效果呢?本文就重点讨论并给出 ...