Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

class Solution {
public:
int removeDuplicates(int A[], int n) {
if(!n)return NULL;
int num=1,i;
for(i=1;i<n;++i)
if(A[i]!=A[i-1])
A[num++]=A[i];
return num; }
};

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

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.

 

 ii.接上题,增加一个count变量来记录key出现的次数。

class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n == 0)
return 0; int key = A[0];
int count = 0;
int start = 0; for(int i = 0; i < n; i++)
if (key == A[i])
count++;
else
{
for(int j = 0; j < min(2, count); j++)
A[start++] = key;
key = A[i];
count = 1;
} for(int j = 0; j < min(2, count); j++)
A[start++] = key; return start;
}
};

  

Remove Duplicates from Sorted Array I&&II——怎样防超时的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  2. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. 【leetcode】Remove Duplicates from Sorted Array II

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

  4. 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 ...

  5. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

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

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

  7. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

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

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

  9. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

随机推荐

  1. 解题:POI 2009 Lyz

    题面 板板讲的霍尔定理 霍尔定理:一张二分图有完全匹配的充要条件是对于任$i$个左部点都有至少$i$个右部点与它们相邻.放在这个题里就是说显然最容易使得鞋不够的情况是一段连续的人,那就维护一下最大子段 ...

  2. Python之旅:装饰器

    装饰器就是闭包函数的一种应用场景 一.为何要用装饰器 开放封闭原则:软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案:能够在不修该 ...

  3. 最近遇到的DISCUZ一些问题解决方法

    “抱歉,您的请求来路不正确或表单验证串不符,无法提交” 打开“source\class\helper\helper_form.php”, 然后把“$_GET[‘formhash’] == formha ...

  4. SGU179 Brackets light

    179. Brackets light time limit per test: 0.25 sec. memory limit per test: 131072 KB input: standard  ...

  5. angular 的 @Input、@Output 的一个用法

    angular 使用 @input.@Output 来进行父子组件之间数据的传递. 如下: 父元素: <child-root parent_value="this is parent ...

  6. [linux]linux调度策略对io的影响

    题记:原本要写nfs的I/O小测试的,但考虑到只是的完整性和系统性,转一篇调度策略相关文章. 1.linux中的调度策略 参考:http://scoke.blog.51cto.com/769125/4 ...

  7. STL源码分析-traits

    http://note.youdao.com/noteshare?id=b5fd9f936cd133af3790a8b0e9c35b8a

  8. bzoj 1004 组合

    代码: //根据Burnside定理:有m个置换k钟颜色,所有本质不同的染色方案数就是每种置换的不变元素的个数的平均数.所谓不变元素就是一种染色方案 //经过置换变换后和之前一样.所以现在就是要求不变 ...

  9. [吴恩达机器学习笔记]13聚类K-means

    13.聚类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 13.1无监督学习简介 从监督学习到无监督学习 在一个典型的监督学习中,我们有一个有标签的训练集,我们的目标是找到能够区分正样本和负 ...

  10. CSS3方法总汇

    PS:CSS3的3D和我做研发时的3D不一样他们只能旋转180度  横为X竖为Z高为Y transfrom:2D3D转换 rotareX:绕着X轴旋转 rotareY(-180deg):绕着Y轴旋转- ...