[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往后遍历 ...
随机推荐
- pyqt4学习资料
官方文档: http://pyqt.sourceforge.net/Docs/PyQt4/classes.html 啄木鸟社区:https://wiki.woodpecker.org.cn/moin/ ...
- 【php】php 生僻知识点认知
资料引用来源:http://www.runoob.com/php/php-tutorial.html 表单提交中, 变量名中的点和空格被转换成下划线.例如 <input name=" ...
- python七类之字典详解
一.字典 一.关键字:dict 1.字典是唯一的键值对数据,其表现形式: dic = {键:值},字典里的键必须保证是唯一的 2.键必须是不可变的数据类型: a.故列表是不能当键的 b.所 ...
- spoj1026 favorite dice
#include <bits/stdc++.h> using namespace std; int n,t; ; double dp[N]; /* 甩一个n面的骰子,问每一面都被甩到的需要 ...
- spring boot打包问题
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories ...
- python中矢量化字符串方法
- webpack入门概念
一 概念 1 入口(entry) 入口起点(entry point)提示webpack 应该使用那个模块,来作为构建其内部依赖图得开始.进入入口七点后,webpack 会找出那些模块和库是入口起点(直 ...
- Odoo8中安装新模块找不到的问题
为了要让系统识别出新的模块,我们需要打开用户的技术特性选项,具体在 左侧栏目->用户->administrator, 将技术特性勾选上,刷新. 然后左侧栏目->模块下面就会 ...
- 新版IdFTP解决中文乱码问题
用XE10后开发FTP客户端,发现有中文乱码问题.这里也主要是编码的问题,在connect链接后,需要设置编码方可. 注意: IndyTextEncoding_OSDefault; 该代码可能需 ...
- eclipse返回快捷键
1.图上第一个箭头(Ctrl + Q) 返回上一个编辑点(编辑,修改代码) 2.图上第二个箭头(Alt + Left) 返回上一个操作点(点击进入方法等操作) 3.图上第三个箭头(Alt + Righ ...