【Leetcode】【Medium】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]
.
解题思路1:
遍历A,i表示当前遍历到的数组下标,length表示当前已重新填充的数组下标;设置一个flag用于记录是否已经出现重复;
从i=length=1开始(数组首元素一定不用移除),当A[i] = A[i-1],如果flag为true,则说明A[i-1]=A[i-2],因此A[i]需要移除(不对数组做更新操作);
当A[i] = A[i-1],如果flag为false,则说明A[i-1]!=A[i-2],因此在当前length位置处使A[length] = A[i],length++,flag不变;
当A[i] != A[i-1],A[length] = A[i],length++,flag = false;
最终length指向新数组最后一个元素的下一个位置,因此数值刚好等于新数组个数,直接返回;
解题思路2:
由于代码中有大量的if else 判断语句,非常难看,因此突然想到,直接判断A[i] 是否等于 A[i-2]就可以了,这样可以直接判断出是否连续三个及以上重复,如果重复了3个以上,就不添加到新数组队列中;
但是,由于从i=length=2开始,判断if (A[i] == A[i-2]),A[i-2]有可能已经是新数组的元素,即有可能已经更新的元素值,原数组被破坏了,就无法正确判断是否连续3个;
对于这种向前判断无法成功的例子,往往可以尝试向后判断,即从i=length=0开始,判断if (A[i] == A[i+2]);
至此,问题很轻松的解决了,而且代码行数非常短,由于判断if (A[i] == A[i+2]) 相当于已经判断了最后三个元素的有效性,因此最后的两个元素一定是合法的,不用移除;
代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= )
return n;
int length = ; for (int i = ; i < n - ; ++i) {
if (A[i] != A[i+])
A[length++] = A[i];
} A[length++] = A[n - ];
A[length++] = A[n - ];
return length;
}
};
【Leetcode】【Medium】Remove Duplicates from Sorted Array II的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 使用express创建node服务器的两种方法及区别
使用express创建node服务器有两种方法,如下所示: 方法一: var express = require('express'); var app = express(); app.listen ...
- Web性能优化之雅虎军规
相信互联网已经越来越成为人们生活中不可或缺的一部分.Ajax,flex等等富客户端的应用使得人们越加“幸福”地体验着许多原先只能在C/S实 现的功能. 比如Google机会已经把最基本的office应 ...
- PHP设置时区
<?php//设置默认的时区date_default_timezone_set('Asia/Shanghai');//输出1396193923对应的日期echo date("Y-m-d ...
- 初识unitest框架
1.借助IED录制脚本 2.导出脚本,选择用Python语言 将脚本导出,保存为 baidu.py ,通过 python IDLE 编辑器打开 引入unittest框架解释,见代码的的注释 # -*- ...
- CodingLife的CSS样式整理
1 首页的超链接鼠标悬停效果 .postTitle a:hover { color:red; text-decoration:none } 2 正文标题鼠标悬停效果 #topics .postTitl ...
- DIV+CSS常见面试题
1.!important拥有最高的优先级,几乎所有浏览器都支持!important,除了IE6(不完全支持) 例1(IE6支持,颜色为#e00): .cssClass{color:#e00!impor ...
- 8086处理器的无条件转移指令——《x86汇编语言:从实模式到保护模式》读书笔记13
本博文是对原书8.3.10的内容的总结. 一.相对短转移 指令格式是: jmp short 标号 标号也可以替换成具体的数值(标号和数值是等价的),例如 jmp short 0x2000 说明: (1 ...
- git忽略ssl认证
问题 在是用git克隆仓库的时候,报错如下: fatal: unable to access ‘https://github.com/........../‘: OpenSSL SSL_connect ...
- Expression Blend实例中文教程(7) - 动画基础快速入门Animation
通过前面文章学习,已经对Blend的开发界面,以及控件有了初步的认识.本文将讲述Blend的一个核心功能,动画设计.大家也许注意到,从开篇到现在,所有的文章都是属于快速入门,是因为这些文章,都是我曾经 ...
- Backup Log
使用Backup Log 命令可进行数据库的事务日志备份.其语法格式如下: Backup 数据库名 To 备份设备 和备份数据库操作一样