一.题目描写叙述

二.解题技巧

这道题和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. ST和LCA和无根树连接

    #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm& ...

  2. 细说 iOS 消息推送

    APNS的推送机制 与Android上我们自己实现的推送服务不一样,Apple对设备的控制很严格.消息推送的流程必需要经过APNs: 这里 Provider 是指某个应用的Developer,当然假设 ...

  3. 轻松学习Linux之Shell预定义变量

    本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  4. Scala——构造函数

    Scala的构造函数分为主构造函数和辅助构造函数. 辅助构造函数 辅助构造函数比较容易理解,它们同C++和Java的构造函数十分类似,只有两处不同: 1.辅助构造函数的名称为this,这主要是考虑到在 ...

  5. 使用Multiplayer Networking做一个简单的多人游戏例子-1/2

    原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 本文主要讲述了如何使用Multiplayer Networking开发多人游 ...

  6. eclipse- log 打印跟输出到文件

    1.在eclipse中打印log,经常使用的就是log.e(string,string) 代码中如下 @Override public boolean onTouchEvent(MotionEvent ...

  7. android设置Activity背景色为透明的3种方

    方法一:这种方法比较简单,只有一个步骤,只需要在配置文件中把需要设置为透明的activity的样式设置为 Android:theme="@android:style/Theme.Transl ...

  8. 14、序列化操作,类的保存和dict转JSON

    在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: d = dict(name='Bob', age=20, score=88) 可以随时修改变量,比如把name改成'Bill',但 ...

  9. [D3] Build a Line Chart with D3 v4

    Line charts are often used to plot temporal data, like a stock price over time. In this lesson we’ll ...

  10. Anaconda的安装

    Windows下Anaconda的安装和简单使用 Anaconda is a completely free Python distribution (including for commercial ...