【Remove Duplicates from Sorted Array II】cpp
题目:
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的更多相关文章
- leetcode 【 Remove Duplicates from Sorted Array II 】python 实现
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【Remove Duplicates from Sorted List II 】cpp
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【Search in Rotated Sorted Array II 】cpp
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【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 ...
- 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 [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
随机推荐
- windows 下设置MTU数值
输入:netsh interface ipv4 show subinterfaces 查询到目前系统的MTU值.再分别输入一行按一次回车键. netsh interface ipv4 set subi ...
- javascript设计模式之装饰者模式
/* * 装饰者模式提供比继承更有弹性的替代方案 * 在不改变原构造函数的情况下,添加新的属性或功能 */ //需要装饰的类(函数) function Macbook() { this.cost = ...
- 更新浏览器,导致编写脚本报错Message: Unable to find a matching set of capabilities
卸载更新浏览器后,所编写的脚本无法运行,报如下的错误:selenium.common.exceptions.WebDriverException: Message: Unable to find a ...
- Visual Studio 2015 终于还是装上了
win8.1系统 vs2015.preview_ult_CHT.iso 大小4.46G, http://download.microsoft.com/download/9/9/1/99133C05-3 ...
- [论文理解]Focal Loss for Dense Object Detection(Retina Net)
Focal Loss for Dense Object Detection Intro 这又是一篇与何凯明大神有关的作品,文章主要解决了one-stage网络识别率普遍低于two-stage网络的问题 ...
- python setup.py install 报错
python setup.py install 报错信息 [root@VM_25_28_centos psutil-2.0.0]# python setup.py install running in ...
- WPF实现ListView大小图标和分组
XAML: <ListView Grid.Row="3" Name="t_lvw_time" HorizontalAlignment="Stre ...
- IntelliJ IDEA中激活JRebel插件
1. 下载激活软件:https://github.com/ilanyu/ReverseProxy/releases/tag/v1.0 我下载的是 2. 双击文件运行 3. 点击change licen ...
- 转:Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
转自:http://blog.csdn.net/business122/article/details/7536991 创建列表 sample_list = ['a',1,('a','b')] Pyt ...
- java,从键盘输入个数不确定的整数,并判断输入的正数和负数的个数,输入0时结束程序。
package study01; import java.util.Scanner; public class Test { public static void main(String[] args ...