Remove Duplicates From Sorted Array
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.
题目解释
给出一个sorted array(意思是指已经排好序了?),处理后数组里每一个元素只能出现一次,返回处理后的数组长度。
不能使用额外的数组空间,只能用已经给出的确定的内存空间。
分析
因为不太懂sorted array具体指的什么,第一次做的时候以为数组是随机的,相同元素出现的位置是随机的,然后题目也没给出limit time,随手就写了一个O(n^3)
for(int i = 0; i < num; i++){
for(int j = i+1; j < num; j++){
if(array[i] == array[j]){
for(int k = j; k < num-1; k++)
array[k] = array[k+1];
num--;
j--;
}
}
}
自然是T了。然后就把sorted array当做已经排好序的数组,那就容易多了,算法也都是O(1),一看代码就明白,水题,直接上代码。
way1
if (nums.empty()) return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
way2 STL
return distance(nums.begin(), unique(nums.begin(), nums.end()));
std::distance
template
typename iterator_traits::difference_type
distance (InputIterator first, InputIterator last);
Return distance between iterators
Calculates the number of elements between first and last.
c++ referencestd::unique
equality (1)
template
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
predicate (2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
Remove consecutive duplicates in range
Removes all but the first element from every consecutive group of equivalent elements in the range [first,last).
c++ reference
相关题目
RemoveDuplicatesfromSortedArrayII
Remove Duplicates From Sorted Array的更多相关文章
- [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
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
随机推荐
- Navigator对象、Screen对象
Navigator对象: Window对象的navigator属性引用的是包含浏览器厂商和版本信息的Navigator对象: Navigator对象集合:plugins[] 返回对 ...
- JavaScript学习笔记-正则表达式(RegExp对象)
正则表达式(RegExp对象) 1.正则表达式字面量,在脚本加载后编译.若你的正则表达式是常量,使用这种方式可以获得更好的性能,重复使用时不会重新编译: 2.使用构造函数创建的RegExp,提供了 ...
- SharePoint2010升级到SharePoint2013操作手册
SharePoint2010升级到SharePoint2013操作手册 目 录 第一章 前言 3 第二章 升级前准备 3 第三章 升级流程图 5 第四章 升级过程 5 4.1 ...
- 无插件的大模型浏览器Autodesk Viewer开发培训-武汉-2014年8月28日 9:00 – 12:00
武汉附近的同学们有福了,这是全球第一次关于Autodesk viewer的教室培训. :) 你可能已经在各种场合听过或看过Autodesk最新推出的大模型浏览器,这是无需插件的浏览器模型,支持几十种数 ...
- [Android]AndroidBucket增加碎片SubLayout功能及AISubLayout的注解支持
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3709957.html 之前写过一篇博客,是使用Fragment来实现T ...
- 【代码笔记】iOS-利用图片序列创建动态图片效果
一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...
- socket编程中客户端常用函数 以及简单实现
1 常用函数 1.1 connect() int connect(int sockfd, const struct sockaddr *servaddr, socklen_taddrlen); 客 ...
- Swift开发第七篇——字面量转换&下标
本篇分为两部分: 一.Swift 中的字面量转换 二.Swift 中的下标 一.Swift 中的字面量转换 所谓字面量就是指像特定的数字,字符串或者是布尔值这样能够直接了当地指出自己的类型并未变量进行 ...
- zend studio 9.0.4 破解、汉化和字体颜色及快捷键相关设置
转载:http://www.penglig.com/post-45.html 下载:http://www.geekso.com/component/zendstudio-downloads/ 破解:h ...
- Hibernate 缓存介绍
Hibernate中提供了两级缓存,一级缓存是Session级别的缓存,它属于事务范围的缓存,该级缓存由hibernate管理,应用程序无需干预:二级缓存是SessionFactory级别的缓存,该级 ...