【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++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
随机推荐
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
本文转自:http://www.trinea.cn/android/preloaddatacache/ 本文主要介绍一个支持自动向前或向后获取新数据的缓存的使用及功能.Android图片内存缓存可见I ...
- tf warning等级
from:http://blog.csdn.net/tsinghuahui/article/details/72938764 tf讨厌的warning 2017-08-03 10:02:52.0990 ...
- Spring IoC和AOP的介绍
基于Spring Framework 版本:5.0.2.RELEASE IoC 概念:传统Java开发中,程序通过new主动创建对象实例,而Spring有专门的IoC容器来创建对象,具体来说就是在Sp ...
- Mysql数据库学习总结(一)
数据库概念 数据库(Database)是按照数据结构来组织.存储和管理数据,建立在计算机存储设备上的仓库. 简单说,数据库就是存放数据的仓库.和图书馆存放书籍.粮仓存放粮食类似. 数据库分类 分为 关 ...
- 全面了解linux情况常用命令
查看linux服务器CPU详细情况1. 显示CPU个数命令 # cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc ...
- mongodb索引 全文索引使用限制
全文索引非常强大,但是同样存在很多限制,我们来看以下去全文索引的使用限制: 1.每次查询,只能指定一个$text查询 2.$text查询不能出现在$nor查询中 之前没有接触过$nor查询,$nor查 ...
- vuejs数据和事件
<body> <div id='root'>{{number}}</div> <script> new Vue({ el:'#root', data:{ ...
- 2018.6.20 Java考试试题总结(Java语言基础与面向对象编程)最新版
Java考试试题总结 一.单选题(每题1分 * 50 = 50分) 1.java程序的执行过程中用到一套JDK工具,其中javac.exe指( B ) A.java语言解释器 B.java字节码编译器 ...
- opencv使用 findContours
http://www.jb51.net/article/132217.htm https://www.jianshu.com/p/4bc3349b4611 https://blog.csdn.net/ ...
- python剑指offer 包含min函数的栈
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). # -*- coding:utf-8 -*- class Solution: def ...