[LeetCode] Remove Duplicates from Sorted Array
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的更多相关文章
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- LeetCode——Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- 数据结构大二课程设计:QT实现线段树
源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...
- linux中shell截取字符串方法总结
截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=wo ...
- typedef使用大全(转)
typedef使用大全(转) 一.数组typedef到处都是,但是能够真正懂得typedef使用的不算太多.对于初学者而言,看别人的源码时对到处充斥的typedef往往不知所错,而参考书又很少,所以在 ...
- rabbitmq使用
1. 用户管理 用户管理包括增加用户,删除用户,查看用户列表,修改用户密码. 相应的命令 (1) 新增一个用户 rabbitmqctl add_user Username Password (2 ...
- Oracle中创建MD5方法
create or replace function MD5(passwd in varchar2) return varchar2 is retval ); begin retval := utl_ ...
- [20160725]ArithmeticTest
public class ArithmeticTest{ public static void main(String[] args) { int [] a={1,3,5,7,9,11,13,15}; ...
- poj 1220(短除法)
http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...
- 6.js模式-中介者模式
1. 中介者模式 所有对象通过中介者进行通信 var playDirector = (function(){ var players = []; var options = {}; options.a ...
- 配置IIS,Apache,PHP过程中遇到的一些问题
下载了eclipse的最新版本,并且添加了PHP插件.为了支持多语言,决定采用UTF-8编码.但是在开发的过程中,发现代码的自动提示帮助信息显示的是乱码,PHP源文件及注释,均正常.在网上查了很多资料 ...
- SuSE Linux 开启VNC服务
一.启动VNC服务输入命令 vncserver 二.编辑启动脚步vi /root/.vnc/xstartup 把twm &注释改为#twm & 然后再最下面增加2行startgnom ...