【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 ...
随机推荐
- urllib库利用cookie实现模拟登录慕课网
思路 1.首先在网页中使用账户和密码名登录慕课网 2.其次再分析请求头,如下图所示,获取到请求URL,并提取出cookie信息,保存到本地 3.最后在代码中构造请求头,使用urllib.request ...
- linux的文件夹权限
r:读取文件夹结构清单的权限,可以列出该文件夹下的所有文件. w:更改目录结构清单的能力,可以新建文件和目录,删除文件和目录(不管这个文件是否属于你),对文件和目录更名,移动文件和目录. x:具有x权 ...
- c# 当前不会命中断点 未载入该文档
C#编码时.有时会遇到标题所说的问题,就是说这个文件和方法明明存在,可总是提示找不到方法.解决方法例如以下: 1.清理全部项目(或相关项目)生成 2.又一次加入全部项目(或相关项目)间的互相引用 3. ...
- ajax 和jsonp 不是一码事 细读详解
由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯 ...
- python is == 的区别, 编码与解码.深浅拷贝
一. is == 的区别 双等表示的是判断是否相等, 注意. 这个双等比较的是具体的值.而不是内存地址 is 比较的是地址 编码回顾 除了了ASCII码以外, 其他信息不能直接转换 编码和解码的时 ...
- MySql 查询列中包含数据库的关键字
MySQL查询列表中包含数据的关键字的处理办法是用``把关键字包起来(tab键上面的字符)
- mysql时间相减的问题
MySQL中时间不能直接相减,如果日.分.时不同,相减结果是错误的 mysql> select t1,t2,t2-t1 from mytest; +--------------------- ...
- LengthFieldBasedFrameDecoder 秒懂
目录 写在前面 1.1.1. 解码器:FrameDecoder 1.1.1. 难点:自定义长度帧解码器 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Java 分布式聊天室[ 亿级 ...
- js apply / call 函数
这两个函数的作用是: 将函数绑定到另外一个对象上去运行 用call和apply应用另一个函数(类)以后,当前的函数(类)就具备了另一个函数(类)的方法或是属性,这也能够称之为“继承”. functio ...
- sin6_addr打印:string to sockaddr_in6 and sockaddr_in6 to string
函式原型: #include <arpa/inet.h> const char *inet_ntop(int af, const void *src, char *dst, socklen ...