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. .oi 小游戏

    http://agar.io/ http://diep.io/ http://slither.io/ http://splix.io/ http://wilds.io/ http://kingz.io ...

  2. Vijos P1769 网络的关键边

    Description 一个连通的无向图,有些点有A属性,有些点有B属性,可以同时具有.删掉某条边后,如果使得连通块中一些点不具有A,B属性的点,求这些边. Sol Tarjan求割边. 首先这些边一 ...

  3. sone2(未完成)

    先留个半完成代码 边看论文边看题解写的...难受.. #include<cstdio> #include<cstring> namespace utils{ inline in ...

  4. Python分割list

    对于一个很大的列表,例如有超过一万个元素的列表,假如需要对列表中的每一个元素都进行一个复杂且耗时的计算,用单线程处理起来会很慢,这时有必要利用多线程进行处理,处理之前首先需要对大的列表进行分割,分割成 ...

  5. C#中委托演变的的三个阶段

    命名函数 匿名方法 lambda表达式 委托是一种可以把引用存储为函数的类型,定义了委托后,就可以声明该委托类型的变量,接着把这个变量初始化为与委托有相同返回类型和参数列表的函数引用,之后就可以使用委 ...

  6. HTML文档中头部文件介绍

    meta是用来在模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和http-eq ...

  7. poj 3268(spfa)

    http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...

  8. centos7.0 安装LNMP运行环境

    LNMP作为php流行的运行环境,而最近需要搭建一个内部的php论坛.记录下LNMP的安装: 1.安装mysql 请参考:centos7 安装mysql5.7.11注意事项 2.安装php yum i ...

  9. Unity3d《Shader篇》地球旋转上空悬浮云层

    记得把纹理设置成Repeat Shader "Custom/Earth" { Properties { _MainTex ("Base (RGB)", 2D) ...

  10. Es6 学习笔记

    变量 let let用来声明变量,作用和var类似,所声明的变量只在let生命的代码块内有效. //1.不允许重复声明 let num = 2; let num = 3; //error //2.块级 ...