LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
解答
这题又是一遍AC。。。只能说LeetCode上水题有点多,这居然都是Medium难度。。。
拿一个变量计数一下重复次数就可以了,每次遇到新的数就将这个变量赋值为1,如果这个变量达到2且遇到和之前重复的数,就移除。
下面是AC的代码:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 0){
return 0;
}
int last = nums[0];
int count = 1;
int dup = 1;
for(vector<int>::iterator iter = nums.begin() + 1; iter != nums.end(); iter++){
if(*iter == last){
if(dup < 2){
dup++;
count++;
}
else{
nums.erase(iter);
iter--;
}
}
else{
dup = 1;
count++;
last = *iter;
}
}
return count;
}
};
118
LeetCode OJ 80. Remove Duplicates from Sorted Array II的更多相关文章
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- spring 配置文件的头部 xmls
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- Shiro 五张表
参考博客: http://blog.csdn.net/frankcheng5143/article/details/50836619 Filter:运行过程中改变进入资源的请求和资源返回的响应中的有效 ...
- 第11章 拾遗4:IPv6(1)_报文格式和地址类型
1. IPv4和IPv6协议栈的比较 (1)IPv6取代IPv4,支持IPv6的动态路由协议都属于IPv6协议(如RIPng.OSPFv3). (2)Internet控制消息协议IPv6版(ICMPv ...
- MySQL客户端管理
mysql -P3306 -h localhost -u root -p 分别是:端口 服务器 用户名 有密码(这里可以先不用输入,带这个参数表示有密码,密码在打开客户端后输入,也可以在这里直接输入 ...
- day5作业(基本数据类型字符串,列表)
#coding:utf-8'''默写99乘法标 金字塔 必做: 1.昨日选做题 博客中有 http://www.cnblogs.com/linhaifeng/articles/7133357.html ...
- python学习之----获取标签属性
到目前为止,我们已经介绍过如何获取和过滤标签,以及获取标签里的内容.但是,在网 络数据采集时你经常不需要查找标签的内容,而是需要查找标签属性.比如标签<a> 指向 的URL 链接包含在hr ...
- 小数据池 id
1. 小数据池, id() 小数据池针对的是: int, str, bool 在py文件中几乎所有的字符串都会缓存. id() 查看变量的内存地址 # id()函数可以帮我们查看一个变量的内存地址 # ...
- 安全测试3_Web后端知识学习
其实中间还应该学习下web服务和数据库的基础,对于web服务大家可以回家玩下tomcat或者wamp等东西,数据库的话大家掌握基本的增删该查就好了,另外最好掌握下数据库的内置函数,如:concat() ...
- 20.纯 CSS 为母亲节创作一颗像素画风格的爱心
原文地址:https://segmentfault.com/a/1190000014837536 感想: 网格grid 又来了: fr : (剩余空间长度)单位, 1.当(50px,nfr),nfr代 ...
- java接口的学习笔记
1接口 接口是一个特殊的类,在JAVA中,接口是由抽象方法和全局敞亮组成. 在JAVA忠使用INTERFACE定义接口. public class InterfaceDemo { public sta ...