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

典型的双指针问题,维护两个指针不断更新就可以了,代码如下:

  1. class Solution {
  2. public:
  3. int removeDuplicates(vector<int>& nums) {
  4. int sz = nums.size();
  5. if(!sz) return ;
  6. int start = ;//第二个指针
  7. int i = ;//第一个指针
  8. int count = ;
  9. int key = nums[];
  10. for(; i < sz; ++i){
  11. if(nums[i] == key)
  12. count++;
  13. else{
  14. for(int k = ; k < min(, count); ++k)
  15. nums[start++] = key;//更改数组元素,指针前移
  16. key = nums[i];
  17. count = ;
  18. }
  19. }
  20. for(int k = ; k < min(, count); ++k)
  21. nums[start++] = key;
  22. return start;
  23. }
  24. };

LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)的更多相关文章

  1. 26. Remove Duplicates from Sorted Array[E]删除排序数组中的重复项

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

  2. [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

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

  3. LeetCode Remove Duplicates from Sorted Array删除整型数组中的重复元素并返回剩下元素个数

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向开头第一个,e往后遍历相同的 i ...

  4. 26. Remove Duplicates from Sorted Array C++ 删除排序数组中的重复项

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 双指针,注意初始时左右指针指向首元素! class Solutio ...

  5. LeetCode OJ Remove Duplicates from Sorted Array II

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

  6. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  7. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

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

  8. LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

    题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...

  9. [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)

    ①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...

随机推荐

  1. Android—Http连接之GET/POST请求

    在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块.在这个模块中涉及到两个重要的类:HttpGet和HttpPost.    创建步骤:    ...

  2. PHP使用Mongodb

    一.安装Mongodb的PHP扩展 wget http://pecl.php.net/get/mongo-1.2.7.tgz //下载扩展包tar zxvf mongo-1.2.7.tgzcd mon ...

  3. Linux环境Oracle相关操作命令行

        打开Oracle监听:lsnrctl start          关闭监听:   lsnrctl stop 监听关闭那么客户端无法连接 进入sqlplus:sqlplus /nolog  使 ...

  4. LeetCode:验证二叉搜索树【98】

    LeetCode:验证二叉搜索树[98] 题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当 ...

  5. PAT 天梯赛 L1-007. 念数字 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-007 AC代码 #include <iostream> #include <cstdio&g ...

  6. winrar5.0注册码

    新建一个文本文档,输入横线内的内容 ---------------------------------------------------------------------------------- ...

  7. os包方法

    os包中实现了平台无关的接口,设计向Unix风格,但是错误处理是go风格,当os包使用时,如果失败之后返回错误类型而不是错误数量. os包中函数设计方式和Unix类似,下面来看一下. func Chd ...

  8. linux根分区满了如何处理,查找大文件方法

    一:如果linux根分区使用量达到100%,会造成如下现象: root不能登录 系统不能正常启动 二:通过命令查找根分区内的大文件 du -sh /* 2>/dev/null | sort -h ...

  9. GitLab 安装与入门

    GitLab介绍: GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. GitLab拥有与Github ...

  10. 《Maven实战》第10章 使用Maven进行测试

    10.2maven-surefire-plugin插件 [生命周期]的[阶段]与[插件]的[目标]绑定 default生命周期的test阶段:使用单元测试框架运行测试 Maven内置绑定:defaul ...