一.题目描写叙述

二.解题技巧

这道题和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. 【Uva 10163】Storage Keepers

    [Link]: [Description] 你有n(n≤100)个相同的仓库.有m(m≤30)个人应聘守卫,第i个应聘者的能力值 为Pi(1≤Pi≤1000).每个仓库只能有一个守卫,但一个守卫可以看 ...

  2. win8用久了变得非常慢, 磁盘占用100%

    完美解决方式:  直接重装win7 完美解决这个问题 在网上查了非常久也没找到有效方法, 求教

  3. Spring中JDBCTemplate的入门

    Spring是IOC和AOP的容器框架,一站式的框架 连接数据库的步骤:[必须会写] Spring当中如何配置连接数据库? 第一步配置核心配置文件: <?xml version="1. ...

  4. Objective-C基础笔记(9)Foundation常用类NSArray

    NSArray用来存储对象的有序列表,它是不可变的 NSArray不能存储C语言中的基本数据类型,如int.float.enum.struct,也不能存储nil,nil代表数组元素的结束 // // ...

  5. 【Good Bye 2017 A】New Year and Counting Cards

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 是元音字母或者是奇数就递增. [代码] #include <bits/stdc++.h> using namespace ...

  6. OGRE之跳出漫长的编译等待

    当你新建一个OGRE项目时是否发现那漫长的编译等待时间差点儿让你崩溃? 当你改动代码不断进行调试时是否由于那漫长的编译等待而让你烦恼? 假设是,那么请继续往下看,您将受益匪浅. ----------- ...

  7. Jquery+Ajax+Bootstrap Paginator实现分页的拼接

    效果图如下 jsp页面引入bootstrap样式,jquery和bootstrap-paginator.js <link type="text/css" rel=" ...

  8. PatentTips - Use of multiple virtual machine monitors to handle privileged events

    BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VMM) typically runs on a compute ...

  9. 洛谷 P2790 ccj与zrz之积木问题

    P2790 ccj与zrz之积木问题 题目背景 ccj和zrz无聊到了玩起了搭积木...(本题选自uva101,翻译来自<算法竞赛入门经典2>) 题目描述 从左到右有n个木块,编号从0到n ...

  10. HorizontalListView中使用notifyDataSetChanged()和notifyDataSetInvalidated()

    今天在项目中用到了水平ListView控件HorizontalListView,也是我在网上找的个开源HorizontalListView直接在项目中使用.我是把HorizontalListView放 ...