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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

发现一道遗留的easy题,说来惭愧,看见这题第一眼就觉得尼玛这不和remove element 一模一样吗,只是这回是要移除重复的,而且对内存有了O(1)的严格限制。同样要求是 in-place 的操作数组,可是我居然忘了当时remove element是怎么做的了。。。相当时我还写出三种解法的啊,怎么现在都想不起来了=_=

仔细回顾了一下之前的博客,终于回想起来了。时间复杂度为O(n) 空间为O(1) 。大体思路就是追踪当前已经删除元素的数量,然后把后面的元素往前移动。

int removeDuplicates(int A[], int n) {
if (n <= ) return ;
int curdel = ;
int prev = A[];
int len = n;
for (int i = ; i < n; i++) {
int cur = A[i];
if (cur == prev) {
len--;
curdel++;
}else {
if (curdel > ) {
A[i-curdel] = A[i];
}
}
prev = cur;
}
return len;
}

[LeetCode] Remove Duplicates from Sorted Array的更多相关文章

  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 II 有序数组中去除重复项之二

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

  3. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  4. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  5. [LeetCode]Remove Duplicates from Sorted Array题解

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

  6. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  7. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  8. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  9. LeetCode——Remove Duplicates from Sorted Array

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

随机推荐

  1. Vijos 1055 奶牛浴场

    Description 求一个不覆盖指定点的最大子矩阵,\(n,m \leqslant 3\times 10^5,S \leqslant 5\times 10^3\) . Sol 没有名字的算法都叫x ...

  2. [CQOI2011]动态逆序对

    (又是一道树套树……自己真是玩疯了……) (题意略) 从网上也看过题解,好像解法很多……比如CDQ+树状数组,树状数组套主席树,树状数组套平衡树……我用的是树状数组套splay. (我会说是因为我不会 ...

  3. python traceback 变量值

    import sys import traceback import cgitb def handleException(excType, excValue, trace): print 'error ...

  4. Java中流的概念

    http://wapwenku.baidu.com/view/04714847b307e87101f69656.html?ssid=0&from=1086k&uid=0&pu= ...

  5. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  6. 调试WebService

    1.运行WebService的调用程序 2.浏览器中运行asmx,这一步是为了让w3wp.exe出现在下一步的列表中 3.“工具”或“调试”菜单-->附加到进程 (MS为什么把同一功能放在不同的 ...

  7. 引入Fresco

    这里告诉你如何在项目中引入 Fresco. 使用 Android Studio 或者其他 Gradle 构建的项目 编辑 build.gradle 文件: 1 2 3 4 dependencies { ...

  8. 【leetcode】Largest Number ★

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. 【mongo】mongo数据转json时特殊类型处理

    mongo数据库中的有些数据类型是无法用json序列化的,比如ObjectId或者datetime.datetime类型. 可以通过json.JSONEncoder来处理 import json im ...

  10. LightOJ1336 Sigma Function(约数和为偶数的个数)

    Sigma Function Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...