leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述
二.解题技巧
这道题和Remove Duplicates from Sorted Array这道题是相似的。仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法。仅仅只是增加了剔除和第一个元素同样的元素的过程。
还有一个思路是增加一个变量。用于记录元素出现的次数。这题由于是已经排序的数组,所以一个变量就可以解决。假设是没有排序的数组,则须要引入一个hash表来记录出现次数。
三.演示样例代码
#include <iostream>
#include <vector>
class Solution
{
public:
int RemoveDuplicatesII(int A[], int n, int dupNum) // dupNum为同意反复的次数
{
if (n < (dupNum + 1)) return n; // 数组元素过少,无需删除反复数据
int num = dupNum; // 存放删除后数组的元素个数,至少有2个元素
for (int i = dupNum; i < n; i++)
{
if (A[i] != A[num - dupNum])
{
A[num++] = A[i]; // 使用不反复元素替换第num个元素的位置
}
}
// 运行算法后,数组A的前num个元素是所求的一个集合
return num;
}
};
測试代码:
#include <algorithm>
#include "solution.h"
using namespace std;
int main()
{
int removeTime = 2; // 同意数组中每一个元素最多反复的次数
int a[100]; // 定义一个存放100个元素的数组
int n = 100;
for (int i = 0; i < n; i++)
a[i] = rand() % 10 - 5;
sort(a, a + 100); // 要求在运行算法之前数组已经过排序
cout << "原始数组:";
for (int j = 0; j < n; j++)
cout << a[j] << " ";
cout << endl << endl;
Solution remove;
int result_num;
result_num = remove.RemoveDuplicatesII(a, n, removeTime);
for (int k = 0; k < result_num; k++) // 数组a中前result_num个元素是处理后的元素
cout << a[k] << " ";
cout << endl;
cout << "删除反复多于" << removeTime << "次的数据后数组剩余" << result_num << "个元素" << endl;
getchar();
return 0;
}
一个測试结果:
该方法有一定的扩展性,同意元素反复若干次。例如以下面情况元素同意反复最多5次:
还有一种使用二分查找的方法:
class Solution {
public:
int RemoveDuplicatesFromStart(int* A, int n)
{
int NumberOfDuplicates = 0;
int Start = A[0];
for (int Index = 1; Index < n; Index++)
{
if ( Start != A[Index])
{
break;
}
NumberOfDuplicates++;
}
return NumberOfDuplicates;
}
int RemoveDuplicatesFromEnd(int* A, int n)
{
int NumberOfDuplicates = 0;
int Start = A[0];
for (int Index = n - 1; Index > 0; Index--)
{
if (Start != A[Index])
{
break;
}
NumberOfDuplicates++;
}
return NumberOfDuplicates;
}
bool search(int A[], int n, int target)
{
if (n < 1)
{
return false;
}
if (n == 1)
{
if (A[0] == target)
{
return true;
}
return false;
}
if (n == 2)
{
if (A[0] == target)
{
return true;
}
if (A[1] == target)
{
return true;
}
return false;
}
if (A[0] == target)
{
return true;
}
// remove the duplicates
int DuplicatesFromStart = RemoveDuplicatesFromStart(A, n);
if (DuplicatesFromStart == (n - 1))
{
return false;
}
int DuplicatesFromEnd = RemoveDuplicatesFromEnd(A, n);
if (DuplicatesFromEnd == (n - 1))
{
return false;
}
n = n - DuplicatesFromStart - DuplicatesFromEnd;
if (n < 2)
{
return false;
}
A = A + DuplicatesFromStart;
if (A[n / 2] == target)
{
return true;
}
if (A[0] > target)
{
if (A[0] < A[n / 2])
{
return search((A + n / 2), (n - n / 2 ), target);
}
if (A[n / 2] < target)
{
return search((A + n / 2), (n - n / 2), target);
}
return search(A, (n / 2), target);
}
else
{
if (A[0] < A[n / 2])
{
if (A[n / 2] < target)
{
return search((A + n / 2), (n - n / 2), target);
}
return search(A, (n / 2), target);
}
return search(A, (n / 2), target);
}
}
};
四.体会
第一种方法的时间复杂度O(n),空间复杂度O(1),支持in place运算,同一时候有一定的扩展性。
若採用变种的二分搜索算法,其实则是增加了剔除和第一个元素同样的元素的过程,增加了这个过程之后,此时在最差情况下的时间复杂度为O(n)。
leetcode笔记:Remove Duplicates from Sorted Array II的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【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 ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- Word Ladder II [leetcode]
本题有几个注意点: 1. 回溯找路径时.依据路径的最大长度控制回溯深度 2. BFS时,在找到end单词后,给当前层做标记find=true,遍历完当前层后结束.不须要遍历下一层了. 3. 能够将字典 ...
- 利用淘宝ip库限制地区访问
https://sss.one/97.html 利用淘宝ip库限制地区访问 有些应用可能需要对某些地区的用户进行限制访问 在采用才此方法的时候,可以利用一些ip库对访问者的ip进行判断 淘宝ip库地址 ...
- uiautomator——第一个例子:打开浏览器,输入网址
1.在sdk安装目录:E:\Test_Tools\auto_test\app\adt-bundle-windows-x86-20131030\sdk\tools下启动uiautomatorviewer ...
- 事件循环(Event Loop)
1.什么是事件循环? JavaScript为单线程执行的,所以是从上到下依次执行,js分为两个任务,宏任务和微任务 首先执行宏任务(第一次就是执行所有的同步代码),再执行所有的微任务,执行完毕之后再次 ...
- 如何在同一台机器上安装多个MySQL的实例(转)
最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MySQL的实例). 先说下,什么是mysql的多实例,简单的来说就是一台机器上安装了多个mysql的服务 ...
- 设计模式之十二:状态模式(State)
状态模式: 当一个对象的内部状态发生变化时同意改变它的行为. Allow an object to alter its behavior when its internal state changes ...
- 【程序猿笔试面试复习】之中的一个 网络与通信篇(一) 几大网络模型:OSI、TCP/IP、B/S与C/S、MVC结构
9.1网络模型 9.1.1. OSI七层模型 OSI(Open System Interconnection,开放系统互联)七层网络模型称为开放式网络互联參考模型.其为国际标准组织指定的一个指导信息互 ...
- 28. Spring Boot配置方式
转自:https://blog.csdn.net/webzhuce/article/details/54564019
- pt支持的格式
- 【Android开发经验】我们要友好的告诉用户,程序要崩溃了
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 尽管我们的程序在正式上线之前,都会经过严格的測试.从而保证程序的健壮性和良好的用户体验,可是 ...