题目:

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. javascript字符串格式化string.format

    String.prototype.format = function () { var values = arguments; return this.replace(/\{(\d+)\}/g, fu ...

  2. Nginx+Keepalived双主轮询负载均衡

    双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...

  3. js基础笔录

    1.页面中获取对象 document.getElementById("demo") 2.在页面加载时向 HTML 的 <body> 写文本 document.write ...

  4. StackOverflow之旅<2>------{HashMap和Hashtable的区别}

    问题 在Java中HashMap和Hashtable的区别? 哪一个对于多线程应用程序更好? 回答 Hashtable是同步的,加了synchronized锁,而HashMap不是.没有加synchr ...

  5. bzoj1189 [HNOI2007]紧急疏散

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...

  6. 全面了解linux情况常用命令

    查看linux服务器CPU详细情况1. 显示CPU个数命令 # cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc ...

  7. 2017.12.14 Java实现-----图书管理系统

    通过对图书的增删改查操作 用数组实现 Manager类 package demo55; import java.util.*; public class Manager { Scanner sc = ...

  8. 浏览器 DNS缓存与DNS prefetch (DNS预解析)

    浏览器 DNS缓存 浏览器DNS缓存的时间跟DNS服务器返回的TTL值无关. 注:TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间. 浏览器在获取网站域名的实际IP地 ...

  9. java基础编程——树的子结构

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 题目代码 /** * 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一 ...

  10. 巧妙使用JQuery Clone 添加多行数据,并更新到数据库

    WEB代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BatchAdd. ...