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 ...
随机推荐
- 给定 n×n 的实数矩阵,每行和每列都是递增的,求这 n^2 个数的中位数。
#define COL 4 #define ROW 4 int findMedian(int matrix[][COL], int row, int col) { int* arr = new int ...
- 大规模WebGL应用引发浏览器崩溃的几种情况及解决办法
一般的Web应用基本上不会导致浏览器崩溃,写Javascript代码也不需要管理内存资源,基本也不需要考虑内存"泄露"的问题.随着H5的崛起,越来越多的原本在桌面端的软件也改头换面 ...
- Android开发之手把手教你写ButterKnife框架(二)
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52664112 本文出自:[余志强的博客] 上一篇博客Android开 ...
- Android必知必会-发布开源 Android 项目注意事项
如果移动端访问不佳,请使用 –> Github版 1. 合理配置 .gitignore 文件 配置 .gitignore 可以排除一些非必要文件和保护保密信息,下面是在项目根目录下 .gitig ...
- JAVA面向对象-----extends关键字
继承使用extends关键字实现 1:发现学生是人,工人是人.显然属于is a 的关系,is a就是继承. 2:谁继承谁? 学生继承人,发现学生里的成员变量,姓名和年龄,人里边也都进行了定义.有重 复 ...
- [ExtJS5学习笔记]第三十二节 sencha extjs 5与struts2的ajax交互配置
本文地址:http://blog.csdn.net/sushengmiyan/article/details/43487751 本文作者:sushengmiyan ------------------ ...
- JDBC编程-事务编程(四)
事务的概念 事务的概念在我看来是指的是一组sql序列,这个序列是一块执行的单位,要么全部执行,要不全部执行,这样可以很好的对数据库进行并发控制. 因为数据库是多个用户都可以同时操作的,如果多个用户同时 ...
- love~LBJ,奥布莱恩神杯3
时间:2016年6月20日8:00:地点:美国金州甲骨文(Oracle)球馆:事件:G7大战,又一次见证伟大赛季奥布莱恩神杯得主--金州勇士VS克利夫兰骑士...... 可以说,这是NBA以来 ...
- iOS下WebRTC音视频通话(一)
在iOS下做IM功能时,难免都会涉及到音频通话和视频通话.QQ中的QQ电话和视频通话效果就非常好,但是如果你没有非常深厚的技术,也没有那么大的团队,很难做到QQ那么快速和稳定的通话效果. 但是利用We ...
- C语言--测试电脑存储模式(大端存储OR小端存储)
相信大家都知道大端存储和小端存储的概念,这在平时,我们一般不用考虑,但是,在某些场合,这些概念就显得很重要,比如,在 Socket 通信时,我们的电脑是小端存储模式,可是传送数据或者消息给对方电脑时, ...