【Leetcode】【Medium】Remove Duplicates from Sorted Array II
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的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 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 ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- Mac上实现对Python的版本切换
最近朋友邀请我帮忙写个比特币自动化交易程序,要求的平台是Okex,用Python写,之前到是自己学过一点自动化交易,不过是MT5的.看了一下Okex提供的API接口,和MT5不一样,它并没有现成的ID ...
- RealProxy AOP的实现
微软有一篇实现 一下是对于该实现的理解 https://msdn.microsoft.com/zh-cn/library/dn574804.aspx public class DynamicProxy ...
- (转)tune2fs命令详解
tune2fs命令详解(原创) 原文:http://czmmiao.iteye.com/blog/1749232 tune2fs简介 tune2fs是调整和查看ext2/ext3文件系统的文件系统参数 ...
- unittest 几个重要概念
unittest是一个python版本的junit,junit是java中的单元测试框架,unittest实现了很多junit中的概念,比如我们非常熟悉的test case, test suite等, ...
- LinuxShell脚本编程7-for和while
1.for的使用 #! /bin/bash ` do echo $a done 表示:a初始值为1,然后a=a+2的操作,一直到a<=10为止 for((i=1;i<=10;i=i+2)) ...
- CoreJava笔记之线程
程序,进程和线程程序:没有执行的指令序列和相关的数据的集合(如:qq.exe) 如:磁盘上的可执行命令进程:正在执行的程序,进程占用资源(CPU,Memoary,IO)线程:是进程中并发执行的过程(共 ...
- AOP的最佳注入方式——MSIL注入
下载PostSharp(Visual Studio Gallery). 安装一个AOP编译器和引入PostSharp.Aspects(注意安装过程中请使用免费的Express版本),然后初步演示代码: ...
- vi 中插入当前时间
声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站 ...
- 移动前端 HTML5 head
移动前端不得不了解的HTML5 head 头标签(2016最新版) 发表于 2016年10月20日 by 愚人码头 被浏览 875 次 小编推荐:掘金是一个高质量的技术社区,从 ECMAScript ...
- Require.js 源码分析
本文将简单介绍下个人对require.js的源码分析,简单分析实现原理 一.require加载资源的流程 require中,根据AMD(Asynchronous Module Definition)的 ...