本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43835055

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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

思路:

(1)题意为给定已排好序的整数数组,如果数组中某一元素从第i个位置到第j个位置(i<j)出现n次,若n>2,则需将位置为i+2到j位置上的j-i-2个元素移除,并将位置j后续元素分别前移j-i-2个位置;若n<=2,则无需改变;求去除重复元素后数组大小以及所得数组。

(2)该题考查的是对数组的操作。由于数组是已经排好序的,所以从前往后遍历一次数组就可以得到结果。在遍历的过程中,设置变量count记录重复元素的个数,如果遇到当前元素和后续元素相同,则count++,如果count<3,则当前位置元素不变;如果count>=3,则当前位置元素不存入数组,直到遍历到下一个数值不同的元素,将下一个出现的数值不同的元素存储在"遍历数组时count=3"的位置上,这样每次遇到连续出现三次或以上的元素时,总是会保留原数组中两个该元素的值,将多余的元素值用其后续数值不同的元素按顺序替换。最后,遍历完数组即为所得。详情见下方代码。

(3)该题还是比较简单,需要注意的是对改变元素位置合适时机的正确判断。希望本文对你有所帮助。

算法代码实现如下:

/**
	 * @author liqq
	 */
	public static int removeDuplicates(int[] A) {
		if(A==null || A.length==0)
	        return 0;
	    int idx = 0;
	    int count = 0;
	    for(int i=0;i<A.length;i++)
	    {
	        if(i>0 && A[i]==A[i-1])
	        {
	            count++;
	            if(count>=3)
	                continue;
	        }
	        else
	        {
	            count = 1;
	        }
	        A[idx++]=A[i];
	    }  

	    return idx;
    }

Leetcode_80_Remove Duplicates from Sorted Array II的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  3. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

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

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

  5. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  6. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

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

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

  8. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  9. LeetCode OJ Remove Duplicates from Sorted Array II

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

随机推荐

  1. pg备份恢复与设置编码

    psql create database jasygl encoding='UTF8' TEMPLATE = template0; 命令行 备份 pg_dump dabase_name > ba ...

  2. CentOS环境下使用GIT基于Nginx的私服搭建全过程

    阅读本文前你必须预先装好CentOS并且已经安装和配置好Nginx了. 安装GIT私服套件 安装centos6.5-centos7.0 安装nginx yum install -y?git gitwe ...

  3. TCP的TIME_WAIT状态

    主动关闭的Socket端会进入TIME_WAIT状态,并且持续2MSL时间长度,MSL就是maximum segment lifetime(最大分节生命期),这是一个IP数据包能在互联网上生存的最长时 ...

  4. Swift:一个简单的货币转换器App在iOS10中的分析和完善

    这本不算是一个完整的货币转换App,只不过是一个小巧的学习性质的程序.该App覆盖了如下几个知识点: 多国语言的支持 通过网络Api接口读取数据 最后我们来修复一个原来代码中的一个小错误作为完美的收尾 ...

  5. Android TV开发总结(六)构建一个TV app的直播节目实例

    请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...

  6. ROSCon 2016视频和幻灯片发布 ROS机器人操作系统重要参考资料

    ROSCon 2016视频和幻灯片发布 By Tully Foote on 十月19,2016 7:28 AM 全部PPT下载地址:http://pan.baidu.com/s/1gf2sn2F RO ...

  7. 剑指Offer——咪咕笔试题+知识点总结

    剑指Offer--咪咕笔试题+知识点总结 情景回顾 时间:2016.10.09 15:00-16:30 地点:山东省网络环境智能计算技术重点实验室 事件:咪咕笔试 知识点总结 1.Html设置格式贵阳 ...

  8. 剑指Offer——动态规划算法

    剑指Offer--动态规划算法 什么是动态规划? 和分治法一样,动态规划(dynamic programming)是通过组合子问题而解决整个问题的解. 分治法是将问题划分成一些独立的子问题,递归地求解 ...

  9. Java进阶(三十八)快速排序

    Java进阶(三十八)快速排序 前言 有没有既不浪费空间又可以快一点的排序算法呢?那就是"快速排序"啦!光听这个名字是不是就觉得很高端呢. 假设我们现在对"6 1 2 7 ...

  10. 安卓IPC机制之Binder详解

    IPC(Inter-Process Communication,跨进程通信)是指两个进程之间数据交换的过程,因此我们首先必须了解什么是进程,什么是线程. 进程:进程是正在运行的程序的实例,与程序相比, ...