题目:

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. 图像Ping技术

    在CORS出现以前,要实现跨域Ajax通信颇费一些周折.开发人员想出了一些办法,利用DOM中能够执行跨域清求的功能,在不依赖XHR对象的情况下也能发送某种请求.虽然CORS技术已经无处不在,但开发人员 ...

  2. Linux安装Desktop 和 vncserver

    sudo su - #使用 root 账户 yum grouplist #查看所有可用的group yum groupinstall GNOME Desktop #安装 GNOME 桌面 yum -y ...

  3. C# WPF 仿QQ靠近屏幕上方自动缩起功能实现

    碰到了类似需求,但是上网查了一圈发现感觉要么很复杂,要么代码很臃肿,索性自己实现了一个 几乎和QQ是一模一样的效果,而且核心代码只有20行左右. 代码如下: using System; using S ...

  4. 闲聊CSS之关于clearfix--清除浮动

    一,什么是.clearfix 你只要到Google或者Baidu随便一搜"css清除浮动",就会发现很多网站都讲到"盒子清除内部浮动时可以用到.clearfix" ...

  5. idea中使用本地jar包

    一个maven项目中使用到了taobao-sdk-java-auto_1455552377940-20160422.jar包,项目是直接导入的jar包而没用用maven的形式导入,导致在idea编辑时 ...

  6. Socket与TcpClient的区别

    原文:Socket与TcpClient的区别 Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AF ...

  7. Angular.js的自定义功能

    1,自定义指令,在html中输入标签显示想要的指令 html script部分 2,标签中的属性的 有属性值时可以通过函数的参数返回属性值 没有属性值时可以设置属性值(自定义属性值) html部分   ...

  8. chmod 777 修改权限之后,文件夹颜色变绿:解决方案

    修改前: 方法一: ls --color=none #不显示颜色 方法一修改后: 方法二:修改配色 ①安装git +++可用在windows下载之后用ftp传上去:http://pan.baidu.c ...

  9. 使用mybatis的延迟加载

    在某些情况下我们需要使用延迟加载技术来提高我们程序查询的性能,通过减少与数据库的连接操作,做到按需加载,这样达到提高程序性能的目的. 首先需要在全局配置文件(SqlMapConfig.xml)中配置全 ...

  10. Thinkphp 不足之处

    1.报错机制 //控制器里面直接输出如下内容,代码不提示.TP报错机制已经开启 echo $aaaaaa; bbbbbbbbb; eco bbbbbbbb; 正常应该给出以下提示 Notice: Un ...