leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述
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]
解法一
要解该题,只需要在上一题(leetcode解题报告(1):Remove Duplicates from Sorted Array)的基础上,加一个计算重复次数的变量count,再做相应的改动即可。
代码如下:
class Solution{
public:
int removeDulicatesII(int A[],int n){
if(n <= 2) //if n <= 2,then the result is n
return n;
int index = 0; //index of new array
int count = 1; //count the duplicated elements
for(int i = 0; i != n; ++i){
if(A[index] != A[i]){ //not equal,so add this element into new array
A[++index] = A[i];
count = 1; //and reset count
//this is because that A[index] != A[i] means no constantly duplicated elements
}
else if(A[index] == A[i] && count < 2){ //if equal and count < 2
A[++index] = A[i]; //add it
++count; //increment the value of count
}
//if A[index] == A[i] and count >= 2,then skip it
}
return index + 1;
}
}
该算法的时间复杂度为O(n),空间复杂度为O(1)。
代码略长,但是有较好的扩展性。如若将count < 2改为count < 3,结果依然正确。
解法二
如果去掉count,那么每次都要判断当前值、前一值以及下一值的值是否都相等,若相等,说明有连续3个相等的值,那么就不做处理;否则,将该值加入数组。
这引出了一个新的问题:对于第一个元素(下标为0),前一元素就会使下标为负数;对于最后一个元素(下标为n - 1),下一元素的下标会溢出。解决办法是去掉这个边界情况。
代码如下:
class Solution{
public:
int removeDuplicatesII(int A[],int n){
int index = 0;
for(int i = 0; i != n; ++i){
if(i > 0 && i < n - 1 && A[i - 1] == A[i] &&A[i + 1] == A[i])
continue;
A[index++] = A[i];
}
return index;
}
}
该算法的时间复杂度为O(n),空间复杂度为O(1)。
由于去掉了变量count,因此该算法只适用于该题。
解法三
换种思路:将index的值设置为2,遍历从i = 2开始,每次比较A[index - 2]和A[i]是否相等,若不相等,则将该值加入新的数组,并将index加1。
代码如下:
class Solution{
public:
int removeDuplicatesII(int A[],int n){
if(n <= 2)return n;
int index = 2;
for(int i = 2; i != n; ++i){
if(A[index - 2] != A[i])
A[index++] = A[i];
//or:
//A[index] = A[i];
//index += 1;
}
return index;
}
}
注意返回的值。此处返回的是为index,而解法一返回的是index + 1。
该算法的时间复杂度为O(n),空间复杂度为O(1)。
leetcode解题报告(2):Remove Duplicates from Sorted ArrayII的更多相关文章
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- <LeetCode OJ> 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【LeetCode算法-26】Remove Duplicates from Sorted Array
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode记录之26——Remove Duplicates from Sorted Array
国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
随机推荐
- asp.net core-7.在Core Mvc中使用Options
1,添加asp.net core mvc应用程序 2,添加Controllers控制器文件夹,Views视图文件夹 然后在Startup类中ConfigureServices方法中注册一下servic ...
- MySQL5.7主从从配置
主从从,也称为级联主从,数据流向:A(主)->B(从)->C(从从),主从从级联复制. 应用场景 在主从配置的基础上,再增加一个从库,进一步提高数据安全,容灾备份. 读写分离,从库只用于查 ...
- (二十九)JSP之国际化
导入 <%@ taglib url="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 创建三个语 ...
- Abp 领域事件简单实践 <二>
上一篇说的是仓储增删改 的时候会自动触发领域事件. 其实也可以随时触发. 现在在应用层触发. 应用层依赖注入 EventBus public void Trigger() { var e = new ...
- .netcore 上传
BS 上传文件,就是 <input type="file" name="file" /> 这个选择文件之后,浏览器保存了文件路径,上传的时候,把这 ...
- sqlserver使用EF模型经验
sqlserver使用EF模型经验 EF模型使用本人在之前两三年中从没使用过,所以刚开始使用就会踩上许多的坑.今天我不单单说下自己所踩的一些坑与当前公司中使用EF模型设计的理念,即是为我自己做个笔记, ...
- pycharm2019.2永久激活
Pycharm2019.2永久激活Pycharm官网在不到两个月内与2019.7.24更新到最新版本pycharm2019.2,不可说更新不快,对于"喜新厌旧"的我怎能错过新版本呢 ...
- Python练习_函数进阶_day10
1. 1.作业 1,写函数,接收n个数字,求这些参数数字的和.(动态传参) 2,读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么? a=10 b=20 def test5(a,b): p ...
- 9.Redis的Java客户端Jedis
Redis的Java客户端Jedis Jedis所需jar包 commons-pool-1.6.jar jedis-2.1.0.jar 1.Jedis常用操作(jedis中的api 和 我们在 l ...
- 第二章 Django之Django安装(2)
Django 安装 1.官方发布版安装 大多数人会考虑从 http://www.djangoproject.com/download/ 下载安装最新的官方 发布版.Django 使用了 Python ...