【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++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
随机推荐
- Mac 颜色取值
command+shift+4 截图,我靠,我以前不知道 系统自带数码测色计, 选择显示十六位制 command+L 锁定 command+shift+c 复制 简直太方便
- 【extjs6学习笔记】1.10 初始: 定义类
http://www.extjs-tutorial.com/extjs/define-new-class-in-extjs
- Visual Studio 2015 Preview 使用中问题一枚
只要碰到IO读写,文件不存在之类的系统异常,就会崩溃一下给你看看.直接重新VS. 不该有的问题确实存在着???? 正常情况是这样的 直接崩溃时万万不行的!!!!
- pat甲级1139
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- Objective-C 引用计数原理
http://www.cocoachina.com/ios/20160112/14933.html 引用计数如何存储 有些对象如果支持使用 TaggedPointer,苹果会直接将其指针值作为引用计数 ...
- Using Autorelease Pool Blocks
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAut ...
- 面试中常见的 MySQL 考察难点和热点
基本架构 MySQL是典型的三层架构模式,在平常使用中对MySQL问题排查和优化,也应该针对具体问题,从对应的层解决问题 服务层:经典的C/S架构,主要是处理连接和安全验证. 核心层:处理MySQL核 ...
- PAT (Basic Level) Practise (中文)- 1004. 成绩排名 (20)
http://www.patest.cn/contests/pat-b-practise/1004 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入 ...
- js字符串的使用
Javascript的内置功能之一就是字符串连接,如果+号用于两个字符串连接 var s="hello,world" //想要查找给定位置的字符 s.cha ...
- 搭建mock服务器(微信小程序)
搭建mock服务器(微信小程序) 如何在微信小程序使用mock.js实在是个问题,为了完全模拟访问路由和数据,选择在搭建本地mock服务器是一个不错的选择. 以下示例了一个mock服务器的搭建过程以及 ...