题目:

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice? (Medium)

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 1122 and 3. It doesn't matter what you leave beyond the new length.

分析:

仍然采用Remove Duplicates from Sorted Array I 中的双指针思路,只不过增加一个变量count记录出现的次数,两次以内的仍然可以添加的数组中。

代码:

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == ) {
return ;
}
int count = , p = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] == nums[i - ]) {
if (count < ) {
nums[p] = nums[i];
p++;
}
count++;
}
else {
nums[p] = nums[i];
p++;
count = ;
}
}
return p;
}
};
 

LeetCode80 Remove Duplicates from Sorted Array II的更多相关文章

  1. Leetcode80. Remove Duplicates from Sorted Array II删除排序数组中的重复项2

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例  ...

  2. 【leetcode】Remove Duplicates from Sorted Array II

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

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

  4. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  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 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. Centos rpm 卸载

    参考网址: http://blog.sina.com.cn/s/blog_7d12ba3f01014gxv.html

  2. H5C3--文本阴影text-shadow

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. git学习记录——分支管理和多人协作

    在公司里难免会出现多个人一起工作,这就需要构建多个分支派发给多个人去干活 这就产生一个需求,分支管理 分支的创建,合并和删除 其他版本控制系统如SVN等都有分支管理,但是用过之后你会发现,这些版本控制 ...

  4. web服务器--nginx简介

    nginx 介绍Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是一款轻量级的Web 服务器/反向代理服务器及电 ...

  5. ubuntu忘记root密码

    解决办法: 选择GRUB第2个选项(恢复模式) 按e进入编辑模式 将ro recovery nomodeset修改成rw single init=/bin/bash 然后再按F10进入单用户模式,进入 ...

  6. VMware 安装 ubuntu 后安装 VMWare tools

    1.如果 VMware 的安装 VMWare tools 的菜单是灰色, 很可能原因是:    你的 cdrom 被占用着. 关闭系统, 编辑配置, 把cdrom 改为 自动检测. 即不要开始就加载一 ...

  7. swing界面jframe新开线程自动定时刷新

    https://www.cnblogs.com/softidea/p/4411452.html

  8. IntelliJ IDEA 17 创建maven项目

    参考博客: https://yq.aliyun.com/articles/111053# 部署服务器时  没有Tomcat Server选项

  9. (转载) poj1236 - Network of Schools

    看到一篇挺好的代码,适合初学者,转载自 博主 wangjian8006 原地址:http://blog.csdn.net/wangjian8006/article/details/7888558 题目 ...

  10. bzoj1823满汉全席

    2-sat模板 这篇博客写得非常好 传送门 //Achen #include<algorithm> #include<iostream> #include<cstring ...