本文是在学习中的总结,欢迎转载但请注明出处: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. MySQL系列教程(四)

    文件打开数(open_files) 我们现在处理MySQL故障时,发现当Open_files大于open_files_limit值时,MySQL数据库就会发生卡住的现象,导致Nginx服务器打不开相应 ...

  2. Dubbo框架应用之(三)--Zookeeper注册中心、管理控制台的安装及讲解

    我是在linux下使用dubbo-2.3.3以上版本的zookeeper注册中心客户端.Zookeeper是Apache Hadoop的子项目,强度相对较好,建议生产环境使用该注册中心.Dubbo未对 ...

  3. Android的Intent机制详解

    Intent 是一个消息传递对象,您可以使用它从其他应用组件请求操作.尽管 Intent 可以通过多种方式促进组件之间的通信,但其 基本用例主要包括以下三个: 启动 Activity: Activit ...

  4. Android图表库MPAndroidChart(十三)——简约的底部柱状图

    Android图表库MPAndroidChart(十三)--简约的底部柱状图 我们继续上一讲,今天还是说下柱状图,这个图的话应该是用的比较多的,所有拿出来溜溜,先看下效果 我们还是来看下基本实现 一. ...

  5. TOP-N类查询

    Top-N查询 --Practices_29:Write a query to display the top three earners in the EMPLOYEES table. Displa ...

  6. 02_c3p0之c3p0-config.xml配置案例,操作c3p0的jdbcUtil工具类的编写

     c3p0也是一个开源jdbc连接池,我们熟悉的Hibernate和Spring框架使用的都是该数据源. 这里获得数据源使用的方法是:ComboPooledDataSource 它提供的构造方法有 ...

  7. Android简易实战教程--第六话《开发一键锁屏应用2·完成》

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51885687点击打开链接 上一篇,初步开发了这个应用,功能都有了(见http:// ...

  8. Android初级教程初谈自定义view自定义属性

    有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...

  9. linux常用的内核镜像格式

    linux常用的内核镜像格式 Linux内核有多种格式的镜像,包括vmlinux.Image.zImage等. 1.     Linux内核镜像格式 1.1 vmlinux vmlinuz是可引导的. ...

  10. shell入门之函数应用

    最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...