描述

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的更多相关文章

  1. 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 ...

  2. &lt;LeetCode OJ&gt; 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  3. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  4. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  5. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  6. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  7. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. LeetCode记录之26——Remove Duplicates from Sorted Array

    国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...

  9. LeetCode(82)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

随机推荐

  1. PHP正则 正向预查&反向预查

    了解正向预查&反向预查前,我们先要知道正则的2个函数:preg_match_all . preg_replace preg_match_all 可以看文章:点击查看 preg_replace ...

  2. (五)Maven中的聚合和继承

    一.为什么要聚合? 定义:我们在开发过程中,创建了2个以上的模块,每个模块都是一个独立的maven project,在开始的时候我们可以独立的编译和测试运行每个模块,但是随着项目的不断变大和复杂化,我 ...

  3. Java数据结构ArrayList

    Java数据结构ArrayList /** * <html> * <body> * <P> Copyright JasonInternational</p&g ...

  4. EF6.0中出现未找到具有固定名称“System.Data.SqlClient”的 ADO.NET提供程序的实体框架提供程序解决办法

    在多工程项目中,由于EF封装在某一个工程里,那么该项目用于EF相关类库 EntityFramework.dll,以及EntityFramework.SqlServer.dll的引用 那么你一个启动工程 ...

  5. git命令 撤销文件修改

    git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git checkout src/views/useChapter.vue #撤销项目目录 src/views/文件夹 ...

  6. Sublime Text 开发神器相关 插件安装 功能介绍

    无法安装更多见http://blog.csdn.net/freshlover/article/details/44261229/ Sublime Text 3 安装插件管理 Package Contr ...

  7. 监控神器-普罗米修斯Prometheus的安装

    搬砖党的福音:普罗米修斯-监控神器 功能: 在业务层用作埋点系统 Prometheus支持多种语言(Go,java,python,ruby官方提供客户端,其他语言有第三方开源客户端).我们可以通过客户 ...

  8. mysql-8.0.16-winx64的最新安装教程

    最近刚学习数据库,首先是了解数据库是什么,数据库.数据表的基本操作,这就面临了一个问题,mysql的安装,我这里下载的是64位的,基于Windows的,以下是在我电脑上的安装过程,希望可以帮助到大家. ...

  9. socket基本用法

    socket介绍 1.什么是socket socket是应用层与传输层中间的一个软件抽象层,它是一组接口.它把TCP/IP这些复杂的协议统一封装起来 这样我们只要知道如何使用socket就好,就已经符 ...

  10. Android Parcelable 序列化复杂数据结构

    参考博文 http://blog.csdn.net/yangzl2008/article/details/7593226 由于项目需要,Activity之间要传递一个特别复杂的数据结构对象,由于以前序 ...