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 ...
随机推荐
- ProgressBar.js – 漂亮的响应式 SVG 进度条
ProgressBar.js 是一个借助动态 SVG 路径的漂亮的,响应式的进度条效果.使用 ProgressBar.js 可以很容易地创建任意形状的进度条.这个 JavaScript 库提供线条,圆 ...
- Eclipse spket插件 内置js文件
这一篇将怎么在spket内置js文件,而不用用户自己去添加. 1. 在开发的Eclipse的 运行配置将下面几个插件勾选上. 2. 在org.eclipse.ui.startup拓展里执 ...
- ArcGIS Server For Linux 10.2.2安装
1.# yum install Xvfb# yum groupinstall "X Window System"# yum install gettext 2./usr/sbi ...
- ArcGIS Server 10.1发布数据源为ArcSDE(直连)的MXD【转】
因为ArcSDE10.1基本默认直连,所以我们在发布直连的MXD仍然需要注意相关的事宜. 1:保证两台机器都能够访问共享存储的信息 2:确保已UNC路径保存ArcCatalog的文件夹连接,而且直连的 ...
- Nessus常见问题整理
个别问题感谢大学霸__IT达人在Kali中文网的解答. 问题1: Kali自带Nessus产品注册失败 报Error(500):Activation failed 出现这个错误原因很多.其中有一个原 ...
- 获取在线APP的素材图片
1.打开iTunes,搜索并下载APP 2.打开下载的APP的路径 4.对ipa包进行解压 5.找到app,右键"显示包内容"进行查看 6.结果
- 如何自定义ViewGroup
依照惯例,先从一个例子说起. 很简单,3张扑克牌叠在一起显示.这个布局效果该如何实现呢?有的同学该说了,这很简单啊,用RelativeLayout或FrameLayout,然后为每一个扑克牌设置mar ...
- Android属性之build.prop生成过程分析
Android的build.prop文件是在Android编译时刻收集的各种property(LCD density/语言/编译时间, etc.),编译完成之后,文件生成在out/target/pro ...
- Android中的XML解析
在安卓中主要有三种XML文档解析方式:DOM(Document Object Model), SAX(Simple API for XML), PULL 他们的主要特点如下表: 特点 主要类 DO ...
- CGAffineTransform方法汇总
CGAffineTransform是二维的仿射变换,可以进行位移,旋转,缩放,CGAffineTransform实际上是一个矩阵. CGAffineTransform { CGFloat a, b, ...