Remove Duplicates from Sorted Array II leetcode java
题目:
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].
题解:
之前是不允许有重复的。
现在是可以最多允许2个一样的元素。
然后删除duplicate,返回长度。
删除duplicate的方法就是指针的操作。具体方法见代码。
代码如下:
1 public int removeDuplicates(int[] A) {
2 if (A.length <= 2)
3 return A.length;
4
5 int prev = 1; // point to previous
6 int curr = 2; // point to current
7
8 while (curr < A.length) {
9 if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
curr++;
} else {
prev++;
A[prev] = A[curr];
curr++;
}
}
return prev + 1;
}
Reference:http://www.programcreek.com/2013/01/leetcode-remove-duplicates-from-sorted-array-ii-java/
Remove Duplicates from Sorted Array II leetcode java的更多相关文章
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II ——LeetCode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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 ...
- 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 ...
随机推荐
- 最长子回文字符串(Manacher’s Algorithm)
# # 大佬博客: https://www.cnblogs.com/z360/p/6375514.html https://blog.csdn.net/zuanfengxiao/article/det ...
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- android tesseract-ocr实例教程(包含中文识别)(附源码)
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.介绍 快过年了,博主的新应用-屏幕取词之了老花镜的编码工作也在紧锣密鼓的进行中.下面分享一下这个应用 ...
- 如何快速将Linux文件系统迁移到Azure存储
概述 前一段时间一直在给一个客户将原先搭载在Linux(客户使用的是CentOS 7.0)上的NFS快速迁移到Azure存储上,并且为了保证数据完整性还需要另开一个存储做冷备,架构图如下: 通过Cli ...
- 在Asp.net core返回PushStream
最近用asp.net core webapi实现了一个实时视频流的推送功能,在Asp.net中,这个是通过PushStreamContent来实现的. 基于对asp.net core的知识,随手写了一 ...
- delphi 结构体和TList的用法
type PRecord = ^TMyRec; TMyRec = record s: string[8]; i: integer; d: double;end;var MyL ...
- 高速排序C++实现
//高速排序 #include<iostream> #include<functional> #include<Windows.h> using namespace ...
- 在Visual Studio中使用用例图描述系统与参与者间的关系
"用例图"用来描述谁用系统,用系统做什么.用例图不涉及使用细节,只用来描述使用人员和系统的关系,也不涉及行动的顺序.一起来体验. 使用Visual Studio 2012创建解决方 ...
- Delphi XE 5,Rad Studio XE 5 官方下载(附破解),更新 Update 1,Help Update 1
Delphi XE 5 破解,有图有真相 Embarcadero RAD Studio XE5 Update 2 v19.0.14356.6604 (等待破解中...): http://altd.em ...
- oracle监听1067错误的处理
一,oracle监听1067错误的处理修改oracle安装目录D:\DataBase\oracle\product\10.1.0\Db_1\NETWORK\ADMIN\下的 listener.ora和 ...