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

解题思路1:

遍历A,i表示当前遍历到的数组下标,length表示当前已重新填充的数组下标;设置一个flag用于记录是否已经出现重复;

从i=length=1开始(数组首元素一定不用移除),当A[i] = A[i-1],如果flag为true,则说明A[i-1]=A[i-2],因此A[i]需要移除(不对数组做更新操作);

                        当A[i] = A[i-1],如果flag为false,则说明A[i-1]!=A[i-2],因此在当前length位置处使A[length] = A[i],length++,flag不变;

                        当A[i] != A[i-1],A[length] = A[i],length++,flag = false;

最终length指向新数组最后一个元素的下一个位置,因此数值刚好等于新数组个数,直接返回;

解题思路2:

由于代码中有大量的if else 判断语句,非常难看,因此突然想到,直接判断A[i] 是否等于 A[i-2]就可以了,这样可以直接判断出是否连续三个及以上重复,如果重复了3个以上,就不添加到新数组队列中;

但是,由于从i=length=2开始,判断if (A[i] == A[i-2]),A[i-2]有可能已经是新数组的元素,即有可能已经更新的元素值,原数组被破坏了,就无法正确判断是否连续3个;

对于这种向前判断无法成功的例子,往往可以尝试向后判断,即从i=length=0开始,判断if (A[i] == A[i+2]);

至此,问题很轻松的解决了,而且代码行数非常短,由于判断if (A[i] == A[i+2]) 相当于已经判断了最后三个元素的有效性,因此最后的两个元素一定是合法的,不用移除;

代码:

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

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

  1. 【leetcode】Remove Duplicates from Sorted Array II

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

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

  3. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

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

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

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

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

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

  7. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  8. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  9. LeetCode OJ Remove Duplicates from Sorted Array II

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

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

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

随机推荐

  1. Dubbo---初识

    1.概述 1.1 Dubbo是一款高性能.轻量级的java RPC框架: 1.2 Dubbo提供的功能: 面向接口的远程调用: 智能容错.负载均衡: 服务注册.发现: 1.3 Dubbo架构 Prov ...

  2. Java - String中的==、equals及StringBuffer(转自CSDN 作者:chenrui_)

    equals是比较值/对象是否相同,==则比较的是引用地址是否相同. ==  如果是基本类型则表示值相等,如果是引用类型则表示地址相等即是同一个对象 package com.char3; public ...

  3. MVC引入SERVICE层 提高代码重用性 沟通CONTROL和MODEL

    MVC是web开发中常见的程序结构. 简单的mvc结构如下: view层:显示层. control层:业务层,集合了各种action. model层:模型层,一般和数据打交道.简单的sample:一个 ...

  4. Centos 7.0设置/etc/rc.local无效问题解决

    安装centos7以后按照以往习惯修改rc.local添加开机启动命令,但重启后发现无效,再次重启发现依然如故 检查系统rc.local服务运行情况 systemctl | grep "rc ...

  5. 在unity中使用protobuf

    https://www.jianshu.com/p/b135676dbe8d 上面的提供序列化和常见的序列化有点不一样 下面的是常见的序列化的,但是他没有讲怎么用工具生成.proto--->.c ...

  6. java ee的map

  7. xcode配置文件中,Architectures表示的意义

    xcode配置文件中,Architectures表示的意义 ======================== Architecture : 指你想支持的指令集. Valid architectures ...

  8. 在Xcode中手动添加pch文件

    在Xcode中手动添加pch文件: 一: 在工程中新建.pch文件,pch文件名通常用工程名字命名: 二: 在Targets->build Settings->Prefix Header ...

  9. [转]AngularJS 实现 Table的一些操作(示例大于实际)

    本文转自:http://www.cnblogs.com/lin-js/p/linJS.html <!DOCTYPE html> <html> <head> < ...

  10. linux修改用户名和密码

    linux修改用户名和密码 修改root密码:sudo passwd root 修改用户密码(如hadoop) sudo passwd hadoop 修改主机名:sudo vi /etc/hostna ...