题目

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();
}
};

GitHub测试程序源码

LeetCode(80)Remove Duplicates from Sorted Array II的更多相关文章

  1. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  2. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  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 ...

  4. LeetCode(83)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  5. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  6. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 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 ...

随机推荐

  1. Hadoop Hive概念学习系列之HDFS、Hive、MySQL、Sqoop之间的数据导入导出(强烈建议去看)

    Hive总结(七)Hive四种数据导入方式 (强烈建议去看) Hive几种数据导出方式 https://www.iteblog.com/archives/955 (强烈建议去看) 把MySQL里的数据 ...

  2. 桌面应用也可以提供HTTP文件下载

    以往,我们肯定知道,在搞Web应用的时候,我们都可能会遇到提供文件下载的功能需求,比如我以前做的一个客户许可证管理系统,客户购买ERP系统后,通常我们会根据客户的机器的机器码生成一个许可文件,而这个许 ...

  3. JavaEE中一些缩写的含义

    EJB:Enterprise JavaBeans  (企业JavaBeans) XML:Extensible Markup Lauguage (可扩展标记语言) SOAP:Simple Object ...

  4. python中一些函数应用

    items将一个字典以列表的形式返回,因为字典是无序的,所以返回的列表也是无序的. 例如:a = {"a":1,"b":2}    a.items  就是 a ...

  5. python的与或非运算

    真的很重要,栽了个跟头!!!(虽然以前好像知道...) print(True or False and False) print((True or False) and False) # True # ...

  6. CF962E Byteland, Berland and Disputed Cities

    思路: http://codeforces.com/blog/entry/58869. 实现: #include <bits/stdc++.h> using namespace std; ...

  7. 关于php的问题

    $polling_items = db_fetch_assoc("SELECT * FROM poller_item WHERE rrd_next_step<=0 ORDER by h ...

  8. chrome调试之Workspaces

    可通过workspaces来编辑本地文件 workspaces是Chrome DevTools的一个强大功能,这使DevTools变成了一个真正的IDE.Workspaces会将Sources选项卡中 ...

  9. Java URL 中文乱码解决办法

    一. 统一所有的编码格式 (1)JSP页面设置:<%@ page language="java" import="java.util.*" pageEnc ...

  10. 51nod 1096 距离之和最小(水题日常)

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 X轴上有N个点,求X轴上一点使它到这N个点的距离之和最小,输出这个最小的距离之和.   Input 第1行:点的数量 ...