Leetcode_80_Remove Duplicates from Sorted Array II
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 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 ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava Retrofit,因为其简单与出色的性能,也是受到很多人的青睐,但是他和以往的通信 ...
- Python rich comparisons 自定义对象比较过程和返回值
Classes wishing to support the rich comparison mechanisms must add one or more of the following new ...
- Java学习之控制跳转语句
控制跳转语句 控制跳转语句: (1)break:中断的意思 A:用在循环和switch语句中,离开此应用场景无意义. B:作用 a:跳出单层循环 b:跳出多层循环,需要标签语句的配合 (2)conti ...
- java设计模式-----单例设计模式
设计模式是个很高深的东西,我也是略懂皮毛,下面让我用最简洁易懂的语言描述下单例设计模式吧. 一些人总结出来用来解决特定问题的固定的解决方案. 解决一个类在内存中只存在一个对象,想要保证对象的唯一. 1 ...
- 关于查找iOS中App路径时所要注意的一个问题
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
- SpringMVC实现用户登录实例
今天分享一下SpringMVC的一个登陆小案例 准备工作 创建一个Dynamic Web Project(本人是Eclipse) 添加相关的jar包,构建路径 创建springMVC-servlet. ...
- Android中Socket通信之TCP与UDP传输原理
一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是"请求-响应方式",即在请求时 ...
- 16 Content Provider总结
第16天 Content Provider 一, 什么是Content Provider? 内容提供者 Android四大主件之一 :短信记录 通讯录 联系人 自定义 >Content Prov ...
- shell的追踪与调试选项
选项: -n :不执行shell脚本,只检查语法问题.没有问题则没有输出. -v :执行shell脚本前,现将shell脚本的命令输出到屏幕上.输出一段,执行一段. -x :将使用到的所有shell脚 ...
- java操作XML文件--读取内容
先把问题贴出来:编写一个可以解析xml及修改xml内容的工具类 由于我以前做过Android应用程序开发,之前也解析过xml文件,所以,这道题不是很难,这篇文章我先解决第一个问 ...