[Leetcode] 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].
题意:可以保留一个重复的元素。
思路:第一种是和Remove duplicates from sorted array类似的思想。隔一个比较一次,拿A =[1,1,1,2,2,3]举例子,其对比的过程如下



这里可以将第6行、第10行的2改为3,则至多重复的数字出现三次,代码为:
class Solution
{
public:
int removeDuplicates(int A[], int n)
{
if(n<=) return n;
int lo=;
for(int i=;i<n;++i)
{
if(A[i] !=A[lo-])
A[lo++]=A[i];
}
return lo;
}
};
方法二:使用计数器,当两者不相等计数器重置,将A[i]赋值给A[lo]的下一位;相等时但是count不超过2,将A[i]赋值给A[lo]的下一位,其余,仅计数器++,这样就可以成功将不等的情况后的,出现第二个和A[i]相等的情况时,将其也赋值给前面的元素。
class Solution {
public:
int removeDuplicates(int A[], int n)
{
int lo=;
int count=;
if(n<) return n;
for(int i=;i<n;i++)
{
if(A[lo]==A[i])
{
count++;
if(count<)
A[++lo]=A[i];
}
else
{
A[++lo]=A[i];
count=;
}
}
return lo+;
}
};
[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素的更多相关文章
- [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode] 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 (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 80. 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 @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
随机推荐
- cors(Cross-origin resource sharing)跨域资源共享
阮一峰老师的文章(http://www.ruanyifeng.com/blog/2016/04/cors.html)跨域资源共享详解和https://developer.mozilla.org/zh- ...
- Spring IoC的底层技术支持——Java反射机制
我们知道,通过 new XmlClassPathApplicationContext("beans.xml")等方式即可启动容器.在容器启动时,Spring 根据配置文件的描述信息 ...
- Laravel -- Blade模板
{{--流程控制--}} @if($name == '1') this is 1 @elseif($name == '2') this.is 2 @else who am i? @endif @for ...
- C语言:类型、运算符、表达式
看了一天书,有点累了.就写写随笔记录一下今天的复习成果吧. C语言的基本数据类型 数值型:整型数,浮点数,布尔数,复数和虚数. 非数值型:字符. 整数最基本的是int,由此引出许多变式诸如有符号整数s ...
- 算法-----数组------ 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...
- Android ANR 分析
首先贴一下trace 文件 Process: com.oppo.reader PID: 20358 Time: 2933175644_1545041895232 Flags: 0x38d83e44 P ...
- web前端/移动端H5博客专家博客大全--值得收藏的前端技术大牛博客地址
web前端/移动端H5博客专家博客大全--值得收藏的前端技术大牛博客地址 Huang Jie Blog .Com-前端开发 http://www.huangjieblog.com/?feed=rs ...
- 『JavaScript』封装
封装可以被定义为对对象的内部数据表现形式和实现细节进行隐藏.通过封装可以强制实施信息隐藏. 在JavaScript中,并没有显示的声明私有成员的关键字等.所以要想实现封装/信息隐藏就需要从另外的思路出 ...
- 大批量复制Oracle数据表,连带复制主键约束,字段说明以及字段默认值(量产)
DECLARE CURSOR tab_name_cur IS SELECT table_name FROM user_tables ...
- mingw编译opencv2.4.13问题记录
为了在程序中用regex,升级了我的mingw,结果官网上的GCC版本都到6.3了,之前一直用4.8.换了编译器以后,对opencv2.4.10的引用就出了问题:undefined reference ...