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 ...
随机推荐
- 【百度之星2019】Strassen
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6719 在本题中,我们只有两种方法计算两个的矩阵的乘积,第一种为定义法,需要次乘法和次加法.第二种为 ...
- asp.net core-5.控制台读取json文件
1,创建控制台应用程序,应用using Microsoft.Extensions.Configuration; 2,新建一个app.json文件 然后修改app.json的属性 3,生成项目,可以看到 ...
- Angular系列-AngularJs1使用Ace编辑器
Ace编辑器 Ace编辑器是一个嵌入web的代码编辑器,支持语法高亮,自动补全等功能,如果想在页面展示或编辑代码,使用该工具是很合适的. 参考项目地址:https://github.com/ajaxo ...
- Qt里的原子操作QAtomicInteger,有挑战性,使用Q_ATOMIC_INT{nn}_IS_SUPPORTED测试系统是否支持
所谓原子操作,即一系列复杂的操作能一气呵成,中间不被其他的操作打断.这在多线程程序中尤其常见,但要实现这种功能,既要考虑程序的良好设计,又要关心特定平台的体系结构和相关编译器对原子特性的支持程度.所以 ...
- 动手篇:简单的注册界面与防SQL注入(续)
注册前先判断用户名是否已经存在,通过if (SqlHelper.Exists(strSql))查询判断是否已经存在.没存在则进行一个数据插入数据库操作 string strSql1 = string. ...
- (十一)Hibernate中的多表操作(1):单向一对多
一.单向一对多() 案例一(用XML文件配置): 一个班级有多个学生,班级可以查看所有学生的信息. ClassBean.java package bean; import java.util.Hash ...
- (十一)mybatis之整合ehcache缓存
一.二级缓存 大家都知道使用mybatis就要先获取sqlsessionfactory,继而使用sqlsession来和数据库交互,每次只需要使用sqlsession对象提供的方法就好,当我们需要第一 ...
- (十)SpringBoot之web 应用开发-Servlets, Filters, listeners
一.需求 Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet. FilterListene 二.案例 2.1 通过注册 ServletRegistr ...
- 十大经典排序【Java实现,手工作坊式】
终于把排序这个硬骨头,但是又很基础的知识点,自己手撕了一遍!之前,使用Python看着算法导论的书手撕过一遍,印象不是很深刻,容易忘记!好记性不如烂笔头!多自己思考解决问题 1,交换类CAS[最简单] ...
- JDBC 复习1 DBUtil
package dbex; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; impo ...