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 ...
随机推荐
- 『Python基础』第2节: Python简介及入门
一. Python介绍 Python是一门高级计算机程序设计语言,1989年,荷兰的Guido von Rossum创造了它.Guido是是一个牛人,1982年,他从阿姆斯特丹大学获得了数学和计算机硕 ...
- js文件分段上传
前端代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/T ...
- MySQL5.7主从同步配置
主从同步,将主服务器(master)上的数据复制到从服务器(slave). 应用场景 读写分离,提高查询访问性能,有效减少主数据库访问压力. 实时灾备,主数据库出现故障时,可快速切换到从数据库. 数据 ...
- (八) Hibernate中的Session以及事务
HibernateUtil.getSessionFactory().getCurrentSession() 和HibernateUtil.getSession() 的区别: 1.异:getCurren ...
- 数据库及MYSQL基础(3)-JDBC
教学视频链接:https://edu.aliyun.com/course/1694?spm=5176.11400004.0.0.29254768sg2H5P 程序文件链接:https://pan.ba ...
- krpano 全景学习
krpano 切片工具下载 https://krpano.com/tools/ krpano 案例使用 https://krpano.com/examples/usage/#top krpano 是 ...
- powerdesign中逆向工程后name和comment的互换
powerdesign 中,将数据库中可以逆向生成pdm的结构图,比较清晰看到系统的结构, 但假如是db先行的话,一般是db中的每个列中用comment中文注释说明这列是 干什么的,但逆向工程后,会发 ...
- Java 面向对象(二)封装
一.封装(Encapsulation) 1.概述 封装是面向对象编程的核心思想.把对象的属性和行为封装起来,其载体就是类. 面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的, ...
- leetcode-62. Unique Paths · DP + vector
题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 【Zookeeper】分布式锁
一.概述 实现原理 实现代码 一.概述 分布式锁解决方案(目的:为了保证在分布式领域中共享数据安全问题) 数据库实现分布式锁(不推荐.效率特别低) 基于Redis实现分布式锁setNx (非常麻烦考虑 ...