一.题目描写叙述

二.解题技巧

这道题和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的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. [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% ...

  4. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  5. [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 ...

  6. [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 ...

  7. LeetCode OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. [LeetCode#82]Remove Duplicates from Sorted Array II

    Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...

  10. [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 ...

随机推荐

  1. Laravel输出sql语句

    $queries = DB::getQueryLog();

  2. PHP生成二维码方法

    <?php //先下载一份phpqrcode类,下载地址http://down.51cto.com/data/780947require_once("phpqrcode/phpqrco ...

  3. FreeModbus RTU slave & Modbus RTU master

    一.FreeModbus RTU 协议数据格式 FreeModbus RTU是开源的一个协议,并且使用FreeModbus RTU 只能当做从机Slave,RTU协议中的指令由地址码(一个字节),功能 ...

  4. while 循环的理解

    if 与 while 的主要区别:if 只判断和执行一次,而 while 却代表着一个循环,执行多少次,要视情况而定: 两种情况(A.B)都会让循环体执行: while A or B: 两种情况(A. ...

  5. BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...

  6. ftp实现图片上传,文件也类似

    本来用得是easyui得控件 点击按钮实现选择图片得 ,但是老板非得要双击图片框实现上传代码....做个简单得记录 前端代码: 首先,<form>表单要加上 enctype="m ...

  7. 用vue.js的v-for,v-if,computed写一个分页样式

    在学Vue,总想写个分页,先写了一个样式. 主要看思路: 思路简单,得到总页数,判断总页数,循环. 先判断总页数是否需要分页,总页数==1页就不分了. 再判断总页数<11就不用--. 总页数&g ...

  8. TC快速搜索在win10下不可用

    今天突然发现TC的快速搜索在win10下突然不可用,按Ctrl + s 呼出快速搜索栏后半天不响应也无法输入文字.论坛里给出来的建议是将 QuickSearch 2.2.3 升级到 2.2.6,目前插 ...

  9. 【2017 Multi-University Training Contest - Team 7】 Euler theorem

    [Link]:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=765 [Description] 问你a ...

  10. javascript进阶教程第二章对象案例实战

    javascript进阶教程第二章对象案例实战 一.学习任务 通过几个案例练习回顾学过的知识 通过案例练习补充几个之前没有见到或者虽然讲过单是讲的不仔细的知识点. 二.具体实例 温馨提示 面向对象的知 ...