【leetcode刷题笔记】Remove Duplicates from Sorted Array II
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]
.
题解:
设置两个变量:右边kepler和前向游标forward。如果当前kepeler所指的元素和它下一个元素相等,那么向A中存入这两个相等的元素,然后将forward置为kepeler+2,向前移动,如果碰到的元素都跟当前kepeler所指的元素相等,就忽略,否则,将kepeler移向forward所指元素,继续循环;如果当前kepeler所指的元素和它下一个元素不相等,那么就把这个元素直接加入A中,kepeler前移,继续循环。
例如题目中给的数组:1,1,1,2,2,3.
初始kepeler指向第一个1,发现kepeler+1也是一个1,就把forward置为kepeler+2=2,向前探测。探测到index为3的元素时候,发现和kepeler所指的元素不相等,就把kepeler移到索引为3处,继续上述过程,最终得到数组1,1,2,2,3.
代码如下:
public class Solution {
public int removeDuplicates(int[] A) {
if(A == null || A.length == 0)
return 0;
int kepeler = 0,forward = 0;
int NewSize = 0; while(kepeler < A.length){
if(kepeler+1 < A.length && A[kepeler+1] == A[kepeler]){
A[NewSize++] = A[kepeler]; //相同的元素有两个,都放入数组中
A[NewSize++] = A[kepeler];
forward = kepeler+2;
while(forward < A.length && A[forward] == A[kepeler])
forward++;
kepeler = forward;
}
else if(kepeler < A.length){
A[NewSize++] = A[kepeler];
kepeler++;
}
}
return NewSize;
}
}
【leetcode刷题笔记】Remove Duplicates from Sorted Array II的更多相关文章
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 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 [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- 【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 ...
随机推荐
- Windows server2008 搭建ASP接口訪问连接oracle数据库全过程记录
真的是太不easy了,曾经的时候在window server 2003上面搭建了一套asp+oracle的接口系统.就费了好大的劲儿,事实上那会迷迷瞪瞪的也不知道怎么的就弄好了,也懒得管了.OK,从昨 ...
- 微信小程序之云开发一
最近听说微信小程序发布了云开发,可以不需要购买服务器,就能开发小程序和发布小程序,对于动辄千元的服务器,极大的节约了开发成本,受不住诱惑,我就开始了小程序的云开发,目前项目已上线,亲测不收费,闲不住的 ...
- local variable 'xxx' referenced before assignment(犯过同样的错)
这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...
- 识别不了socket未知的名称或服务
个人博客地址:https://blog.sharedata.info/ socket 链接导致java.net.UnknownHostException: R1-T1-N6: R1-T1-N6: 未知 ...
- 在VS中自动生成NuGet包以及搭建自己的或单位内部的NuGet服务器
关于NuGet的介绍已经很多,可以参考下面的: NuGet学习笔记(1)--初识NuGet及快速安装使用 http://kb.cnblogs.com/page/143190/ NuGet学习笔记(2) ...
- 【BZOJ4537】[Hnoi2016]最小公倍数 分块
[BZOJ4537][Hnoi2016]最小公倍数 Description 给定一张N个顶点M条边的无向图(顶点编号为1,2,…,n),每条边上带有权值.所有权值都可以分解成2^a*3^b的形式.现在 ...
- Jeecms 防xss处理原理
Web.xml配置过滤器,并指的要过滤和替换的字符: 过滤器的filter方法,对传入的HttpServletRequest对象进行了修改 具体过滤在XssHttpServletRequestWrap ...
- VMware Integrated OpenStack (VIO)简介
VMware Integrated OpenStack是一款由VMware提供支持的OpenStack发行版软件,用于帮助IT在现有的VMware基础架构之上更加轻松地运行基于生产级OpenStack ...
- LeetCode:范围求和||【598】
LeetCode:范围求和||[598] 题目描述 给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作. 操作用二维数组表示,其中的每个操作用一个含有两个正整数 a ...
- ionic新项目启动步骤
1.sudo npm install -g gulp 2.SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install ...