【LeetCode】83 - 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 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.
Error Solution: runtime error
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()<)return nums.size();
for(vector<int>::iterator iter1=nums.begin(),iter2=nums.begin()+;iter2!=nums.end();iter1++,iter2++){
if(*iter1==*iter2)nums.erase(iter1);//这里erase之后iter1所指向的元素已经不存在了,故iter1已经无效
}
return nums.size();
}
};
Solution: "It doesn't matter what you leave beyond the new length."主要是理解这句话,原vector不需要删除元素,用length保存当前下标,直到找到下一个不一样的元素再++
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()<)return nums.size();
int length=;
for(int i=;i<nums.size();i++){
if(nums[i]!=nums[i-])
nums[length++]=nums[i];
else
continue;
}
return length;
}
};
【LeetCode】83 - Remove Duplicates from Sorted Array的更多相关文章
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode】026. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断相邻节点是否相等 使用set 使用列表 递归 日 ...
- 【LeetCode】83 - Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- ubuntu 12.10无法用apt-get安装软件 Err http://us.archive.ubuntu.com quantal-updates/main Sources 404 Not
之前执行apt-get 不管是什么软件或apt-get update都会遇到fail to fetch http://us.archive.ubuntu.com quantal-updates/ma ...
- LabelMe图像数据集下载
Download MATLAB Toolbox for the LabelMe Image Database 利用Matlab Toolbox工具箱下载图像库 一.下载Matlab Toolbox工具 ...
- Android 定制下拉刷新头部 Ultra Pull To Refresh
我们看到手机中的各种APP的花样繁多的下拉刷新是不是有点心动呢,想着自己定制自己的专门的下拉刷新,市场上比如,58同城,京东,天猫,美团等下拉刷新都是在下拉头部执行帧动画,我最近看到一个APP,就是慕 ...
- POJ 1568 Find the Winning Move(极大极小搜索)
题目链接:http://poj.org/problem?id=1568 题意:给出一个4*4的棋盘,x和o两人轮流放.先放够连续四个的赢.给定一个局面,下一个轮到x放.问x是否有必胜策略?若有,输出能 ...
- Gliffy Diagrams 好用的流程图工具
很好用!加上百度脑图!good!
- 连接Access数据库查询语句
--在使用之前注意将“生成”里的“配置管理器”的“配置平台”改成X86 private void button1_Click(object sender, EventArgs e)//查询 { Ole ...
- Linux删除文件后空间没有释放
.COMMAND默认以9个字符长度显示的命令名称.可使用+c参数指定显示的宽度,若+c后跟的参数为零,则显示命令的全名.PID:进程的ID号.PPID父进程的IP号,默认不显示,当使用-R参数可打开. ...
- [Web前端系列之_Firebug_00_序]
[因] 以前一直把Firebug当做参考他人网站界面结构的工具,看看css,js等,没有深挖.这段时间在项目组里主要充当前台工作,也有空,就准备把前端给精通点,firebug作为入手点. [参考资料] ...
- linux CPU loading calculate
http://hi.baidu.com/lionpanther/item/e908c37abdaf1c3f71442380 #include <stdio.h>#include <s ...
- HDU1010 Tempter of the Bone
解题思路:相当经典的一题,回溯,具体细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> ...