LeetCode(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.
分析
给定一个已排序序列,去除重复元素,使得结果中每个元素的出现次数不超过2;
AC代码
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty())
return 0;
int len = nums.size();
if (len <= 2)
return len;
vector<int> ret;
for (int i = 0; i < len; i++)
{
int temp = nums[i];
int count = 1;
while (i < (len-1) &&nums[i + 1] == temp)
{
i++;
count++;
}
if (count >= 2)
{
ret.push_back(nums[i]);
ret.push_back(nums[i]);
}
else if (count == 1)
ret.push_back(nums[i]);
}//for
nums.clear();
nums = ret;
return ret.size();
}
};
LeetCode(80)Remove Duplicates from Sorted Array II的更多相关文章
- 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
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(83)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- 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 (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】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 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 ...
随机推荐
- 如何让Android微博个人详情页滚动到顶部
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/103 个人详情页滑动到顶部 最近产品提了个新需求,需要实现 ...
- Hexo瞎折腾系列(1) - 准备工作与简单美化
前言 网上有不少相关的帖子,不过版本会比较旧,而不同版本可能存在代码不同的问题,不过大部分还是大同小异,本系列就不啰嗦重复了,基本只会按照本人所使用的版本以及个人所使用到的内容来进行介绍. 该系列是对 ...
- 线段树(单点更新) POJ 2886 Who Gets the Most Candies?
题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...
- eclipse导入php项目
整个工程的都在一个文件夹里面 怎么把它导入到eclipse里面呢:在eclipse里新建一个与要导入的工程同名工程.
- 使用ant build build.xml报“includeantruntime was not set”警告及"Class not found: javac1.8"问题
问题1:ant编译build.xml报“includeantruntime was not set”警告. 警告详情: warning: 'includeantruntime' was not set ...
- Vue2.0实现路由
Vue2.0和1.0实现路由的方法有差别,现在我用Vue 2.0实现路由跳转,话不多说,直接上代码 HTML代码 <div class="tab"> <route ...
- SIRI课程表
wen 周一@0@{今天没课哦}周一 周二今天共一节课 第2节,可编程控制器应用,11号教学楼1 0 3房间 周二 周三今天共三节课 第二节,过程控制系统,2号楼2 1 0房间,第三节机械制造技术11 ...
- Redis为什么这么快
Redis为什么这么快 1.完全基于内存,绝大部分请求是纯粹的内存操作,非常快速.数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1): 2.数据结构简单, ...
- Failed to obtain lock on file /usr/local/nagios/var/ndo2db.lock: Permission denied : Permission denied
Failed to obtain lock on file /usr/local/nagios/var/ndo2db.lock: Permission denied : Permission den ...
- jsp <%@ include %> 例子
<%@ include %>:所有代码包含进来之后一起进行处理,最终编译成一个servlet. jsp文件中添加top和bottom.jsp页面 empList.jsp <%@ pa ...