【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 ...
随机推荐
- SPOJ 78 Marbles 组合数学
相当于从n-1个位置里面找k-1个位置放隔板 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- dojo 二 AMD模块
可参考官方教程:http://dojotoolkit.org/documentation/tutorials/1.7/hello_dojo/教程里主要定义了两个方法,setText设置文本内容和res ...
- linux系统的目录讲解
还记得在前面介绍c h m o d命令时讲过,目录的权限位和文件有所不同.现在我们来看看其中的区别.目录的读权限位意味着可以列出其中的内容.写权限位意味着可以在该目录中创建文件,如果不希望其他用户在你 ...
- 17.allegro导入导出[原创]
一.从一张现成的PCB中导出元件封装到库中 --- -- 二. ①规则 ②元件摆放位置信息导出 这个时候我们在新建的电路板上: ① 导入记事文档 -- -- 到如后: 系统本来默认的是双层,这个时候变 ...
- 错误 -force-32bit 与 ANDROID_EMULATOR_FORCE_32BIT=true
1,配置环境变量, 加上ANDROID_EMULATOR_FORCE_32BIT=true 2,在AS中启动模拟器用下面方法 在你要运行的个工程右击->Run as -> Run conf ...
- H5移动前端完美布局之-margin百分比的使用
一 ,背景 在移动端页面开发我们经常会遇到一件尴尬的事 我们所开发出来的页面跟设计师所给的页面差别很大 再加上移动设备屏幕的大小不一出来的效果更是参差不齐 然后.... 当然 现实情况没有这么糟糕.. ...
- How to: Read Object Data from an XML File
This example reads object data that was previously written to an XML file using the XmlSerializer cl ...
- 利用XPath读取Xml文件
之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...
- 【Todo】MQ学习-RabbitMQ, ActiveMQ, Kafka等
之前学习过RabbitMQ,并且还安装过.安装记录的文章如下: Erlang:http://www.cnblogs.com/charlesblc/p/5512380.html RabbitMQ:htt ...
- eclipse有生成不带参数的构造方法的快捷键吗
你打上类名的2个字母,然后”alt“ +“/” 基本上选第一个就行了