排序数组去重题,保留重复两个次数以内的元素,不申请新的空间。

解法一:

因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断。

Runtime: 20 ms, faster than 19.12% of C++ online submissions for Remove Duplicates from Sorted Array II.

class Solution
{
public:
  int removeDuplicates(vector<int> &nums)
  {
    if (nums.size() == 0)
      return 0;
    int i = 1;
    int lastNum = nums[0];
    int times = 1;
    while (i < nums.size())
    {
      if (nums[i] == nums[i - 1])
      {
        ++times;
        if (times > 2)
        {
          nums.erase(nums.begin() + i);
          continue;
        }
      }
      else
      {
        times = 1;
        lastNum = nums[i];
      }
      i++;
    }
    return nums.size();
  }
};

解法二:

讨论区看到Stefan Pochmann大神的解法,他的解法一如既往的让人眼前一亮,膜拜啦。

Runtime: 8 ms, faster than 100.00% of C++ online submissions for Remove Duplicates from Sorted Array II.

class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
int i = ;
for (int n : nums)
if (i < || n > nums[i - ])
nums[i++] = n;
return i;
}
};

[leetcode] 80. Remove Duplicates from Sorted Array II (Medium)的更多相关文章

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

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

  2. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  3. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

  6. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)

    题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...

  8. Leetcode#80 Remove Duplicates from Sorted Array II

    原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...

  9. [LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值

    和第一题不同的地方是,容忍两次重复 虽然题目上说只需要长度,但是否检测的时候如果数组不跟着改变也是不行的 没说清楚题意 自己是用双指针做的,看了大神的答案更简单 public int removeDu ...

随机推荐

  1. Android开发环境搭建(原创)

    1,我的环境: win8 64位 2,软件安装: 1) jdk-7u40-windows-i586.exe 下载合适的版本,我下载的是 jdk7u40 32位 for windows 安装JDK,配置 ...

  2. jquery选择器集锦

    一,基本选择器: 1 2 3 4 $("#txtName");   $("#txt\\#b");//获取id为 txt#b的元素,\\为转义符 $(" ...

  3. 一、OpenScenGraph环境搭建

    1.OpenSceneGraph 3.4.0  网址  http://www.openscenegraph.org/ github源码地址 https://github.com/openscenegr ...

  4. SYN591-B型 转速表

       SYN591-B型 转速表 光电转速表数显转速表智能转速表使用说明视频链接: http://www.syn029.com/h-pd-249-0_310_44_-1.html 请将此链接复制到浏览 ...

  5. 《Effective Java》-——用私有构造器或者枚举类型强化Singleton属性

    Singleton指仅仅被实例化一次的类.Singleton通常被用来代表那些本质上唯一的系统组件,比如窗口管理器或者文件系统.使类成为Singleton会使它的客户端测试变得十分困难,因为无法给Si ...

  6. Spring Boot使用MyBatis Generator、Swagger

    MyBatis是Java目前主流的ORM框架,在Spring Boot中使用MyBatis可以参考这篇文章:http://www.ityouknow.com/springboot/2016/11/06 ...

  7. Spring 入门程序

    1.0 导包的时候要注意: 以上的第一个是.class文件 以上的第二个是文件的解释性页面. 以上的第三个是.java文件 2.0 配置文件需要导入依赖(有dtd 依赖,也有xsd依赖) ²  从be ...

  8. Linux虚拟机怎么添加磁盘?

    一.VMware workstation菜单栏

  9. .Net 通过设置Access-Control-Allow-Origin来实现跨域访问

    目录 # 前言 # 为每个API接口单独添加响应头 1.针对 ASP.NET MVC 项目的Controllers 2.针对 ASP.NET Web API项目的Controllers 3.针对ASP ...

  10. 02(b)多元无约束优化问题-最速下降法

    此部分内容接02(a)多元无约束优化问题的内容! 第一类:最速下降法(Steepest descent method) \[f({{\mathbf{x}}_{k}}+\mathbf{\delta }) ...