题目

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. 最短路之Floyd(弗洛伊德)

    只有五行的Floyd最短路算法: 核心代码 每次都更新通过k点,然后从i到j的最短路程...

  2. 在img标签上尽量不要使用onerror事件

    在img标签上尽量不要使用onerror事件 因为在之前的时候,我在本地对用户头像修改发现,如果图片加载失败, 使用onerror事件去获取一个默认地址的图片虽然这是可行的,但是如果刚好onerror ...

  3. div倾斜 文字不倾斜

  4. CSS选择器优先级【转】

    样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...

  5. Caffe实战一(环境准备及CPU模式下编译)

    经过前几天的折腾,终于把Ubuntu16.04开发环境给搭建了起来,包括win10+Ubuntu双系统的安装.系统安装后的优化等等. 详见之前的文章:Ubuntu16.04.2 LTS 64bit系统 ...

  6. Palindromes in a Tree CodeForces - 914E

    https://vjudge.net/problem/CodeForces-914E 点分就没一道不卡常的? 卡常记录: 1.把不知道为什么设的(unordered_map)s换成了(int[])s ...

  7. 第05课 Linux命令初探(一)

    该篇为第一部分,主要介绍的Linux指令有: mkdir.cd.ls.rm.touch.vi/vim.echo.cat.cp.mv.pwd.rm.rmdir 1.创建一个目录/data 提示:Wind ...

  8. Hu矩

    close all; clear all; I1=imread('lena.bmp'); angle=; T=[cos(angle),sin(angle),;-sin(angle),cos(angle ...

  9. Android应用的安全隐患*

    转自: http://www.cnblogs.com/chi0591/p/3864747.html Android应用的安全隐患包括三个方面: 代码安全 数据安全 组件安全 代码安全 代码安全主要是指 ...

  10. client系列、offset系列、scroll系列

    一.client系列 clientWidth/clientHeight    是我们设置的宽和高加上内边距(没有边框) clientLeft/clientTop 就是我们设置的边框值 二.offset ...