【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 ...
随机推荐
- Data Base sqlServer 组合主键
sqlServer 组合主键 创建表时: create table Person ( Name1 ) not null ,Name2 ) not null primary key(Name1,Na ...
- ftrace的使用【转】
转自:http://blog.csdn.net/cybertan/article/details/8258394 This article explains how to set up ftrace ...
- jpa+spring配置多数据源
property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useU ...
- CSS 中浮动的使用
float none 正常显示 left 左浮动 right 右浮动 clear none 允许两边浮动 left 不允许左边浮动 right 不允许右边浮动 both 不允许两边浮动 <!DO ...
- 【笨嘴拙舌WINDOWS】Dj,oh!nonono,It is about DC
“DC: Device content 设备描述表.通常指显示器,或者打印机设备的描述” 如果你不是从事打印机方面的编程,那么就可以将DC简单的理解为显示器的属性表.WINDOWS将内存里面的东西通过 ...
- POJ (Manacher) Palindrome
多敲几个模板题,加深一下对Manacher算法的理解. 这道题给的时间限制15s,是我见过的最长的时间的了.看来是为了让一些比较朴素的求最大回文子串的算法也能A过去 Manacher算法毕竟给力,运行 ...
- Codeforces Round #276 (Div. 2)
A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...
- UISegment
UISegment分段控制 属性 1.segmentedControlStyle 设置segment的显示样式. typedef NS_ENUM(NSInteger, UISegmentedContr ...
- 使用dev http client调试restful API开发
安装chrome 插件:dev http client, 使用VPN打开 google网站,
- PHP设计模式之装饰者模式
<?php /* 装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. */ header("Content-type:text/html; cha ...