题目

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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

代码

class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= ) return n;
int index = ;
for (int i= ; i<n; i++)
{
if ( A[index-]!=A[i] )
{
A[index++] = A[i];
}
}
return index;
}
};

Tips:

1. index始终指向下一个要插入元素的位置

2. 判断当前元素与index-2位置元素是否相等,如果不等就可以插入,保证没有连续三个相同的元素

3. 这里用到些数学归纳法的技巧:

  a. 只要保证第1-第3个元素不是都相同的

  b. 并且再后面每一步添加元素的时候判断都不是相同的

 则可得结论一直到条件结束,调整后的数组中不会有三个连续重复的元素

========================================

第二次过这道题卡住了一下,复习了第一次写的程式,改出了AC的代码。

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

tips:

这种数学归纳法之类的去重代码,可以总结出一定的规律

第一个指针 prev = 可以保持的重复数量 (这个指针指示当前可插入的位置)

第二个指针 i 从 可以保持重复数量下标开始 一直到数组长度最后

去重条件判断nums[prev-可以保持重复的数量]!=nums[i]

这样就可以获得满足题意的程式。

================================

这里还有一种特殊的case,如果出现重复的就不要了呢?写出了下面的程式

// special case
class SolutionS{
public:
static int removeDuplicates(vector<int>& nums)
{
if ( nums.size()< ) return nums.size();
int i=;
int prev = nums[]!=nums[] ? : ;
while (i<nums.size()-)
{
if (nums[i]!=nums[i-] && nums[i]!=nums[i+])
{
nums[prev++]=nums[i];
}
i++;
}
if ( nums[i]!=nums[i-] ) nums[prev++]=nums[i];
return prev;
}
};

这个程式是自己写的,自测了一些case。

思路很朴素:

1. 维护一个prev作为待插入的位置

2. 判断一个元素与前后元素是否相等

3. 处理第一个和末尾元素(第一个没有前驱,末尾没有后继,所以要特殊处理)

====================================================

还有一种情况,如果数组不是排序的,就用hashtable记录每个数字出现的次数。

【Remove Duplicates from Sorted Array II】cpp的更多相关文章

  1. leetcode 【 Remove Duplicates from Sorted Array II 】python 实现

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

  2. 【Remove Duplicates from Sorted List II 】cpp

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  3. 【Search in Rotated Sorted Array II 】cpp

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  4. leetcode 【 Remove Duplicates from Sorted List II 】 python 实现

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  5. 【leetcode】Remove Duplicates from Sorted Array II

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

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

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

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

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

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

随机推荐

  1. 菜鸟 学注册机编写之 “MD5”

    测试环境  系统: xp sp3 调试器 :od 1.10 sc_office_2003_pro 高手不要见笑,仅供小菜玩乐,有不对或不足的地方还请多多指教,不胜感激! 一:定位关键CALL 1. 因 ...

  2. AngularJs Type error : Cannot read property 'childNodes' of undefined

    参考博客: https://blog.csdn.net/u011127019/article/details/73087868 在AngularJs和JQuery插件共存咋项目中经常会遇到如下异常 T ...

  3. Unity中的输入

    目录 移动平台的输入 触摸 触摸相关的函数 触摸的一个示例 重力加速器 在Unity中访问重力加速器的信息 重力加速器示例 虚拟键盘 其他输入 传统的输入 鼠标,键盘,控制杆,手柄 虚拟控制轴(Vir ...

  4. 跨平台移动开发phonegap/cordova 3.3全系列教程-简介

    一.   跨平台實現架構: phonegap +asp.net+jquery mobile/jqmobi 二.   PhoneGap简介 PhoneGap是一个开源的开发框架,用来构建跨平台的使用HT ...

  5. 新增自定义聚合函数StrJoin

    1.添加程序集Microsoft.SqlServer.Types CREATE ASSEMBLY [Microsoft.SqlServer.Types] AUTHORIZATION [sys] FRO ...

  6. mysqlbench使用

    看见不少人问mysqlbench怎么用,这个好像没什么困难的,基本看的懂英文就可以使用了,感觉像使用word一样. 下载地址http://www.mysql.com/products/workbenc ...

  7. Bezier贝塞尔曲线的原理、二次贝塞尔曲线的实现

    Bezier曲线的原理 Bezier曲线是应用于二维图形的曲线.曲线由顶点和控制点组成,通过改变控制点坐标可以改变曲线的形状. 一次Bezier曲线公式: 一次Bezier曲线是由P0至P1的连续点, ...

  8. Codeforces Round #324 (Div. 2) A B C D E

    A,水题不多说. #include<bits/stdc++.h> using namespace std; //#define LOCAL int main() { #ifdef LOCA ...

  9. Zend Studio 12.5.1原版安装破解

    安装官方Zend Studio 12.5.1原版,关闭zend studio,然后将破解补丁com.zend.verifier_12.5.1.v20150514-2003.jar覆盖到 安装目录\pl ...

  10. SpringBoot学习记录(二)

    一. SpringBoot日志框架 SpringBoot:底层是Spring框架,Spring框架默认是用JCL(commons-logging): SpringBoot选用SLF4j和logback ...