leetcode解题报告(1):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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
分析
要去掉有序数组中的重复数字,需要设置一个索引index,该索引初始化为第一个元素下标,用于与后续数组元素比较。若相等,则跳过,否则,将index下标加1,并使当前比较的元素的下标等于该索引值,直到最后一个元素。由于index为数组下标值,因此将其加1后才是数组长度。
解该题的思想为:将所有检查到的不重复的元素的下标改为一段连续的数值以作为新的下标。
以题述为例:index初始化为0,即A[0],将其与第二个元素A[1]比较,相同,跳过;再与第三个元素A[2]比较,不同,则将index加1,即以A[++index](此时index的值为1)作为元素A[2]的新下标。然后发现数组遍历已结束,退出。至此,就得到了一个不重复的有序数组。
该解法的时间复杂度为O(n),空间复杂度为O(1)。
代码如下:
class Solution{
public:
int removeDuplicates(int A[],int n){
int index = 0;
for(int i = 1; i != n; ++i){ //Note:the for loop begins from the second element in the array
if(A[index] != A[i]) //if not equal
A[++index] = A[i]; //modify index of the original elements
//if equal,do nothing(which means not changing the value of index)
}
}
return index + 1; //the result is index plus 1
}
使用STL
如果使用STL,可以考虑distance和unique这两个函数。
distance函数接受两个迭代器参数,该函数返回两个迭代器之间的距离(即长度);
unique函数同样接受两个迭代器参数,该函数去除迭代器范围内的重复元素,并返回最后一个元素的尾后指针。
使用这两个算法的代码如下:
class Solution{
public:
int removeDuplicates(int A[],int n){
return distance(A,unique(A,A + n));
}
}
该解法的时间复杂度为O(n),空间复杂度为O(1)。
leetcode解题报告(1):Remove Duplicates from Sorted Array的更多相关文章
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 【LeetCode算法-26】Remove Duplicates from Sorted Array
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode记录之26——Remove Duplicates from Sorted Array
国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...
- LeetCode(80)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
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 【leetcode❤python】26. Remove Duplicates from Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def removeDuplicates(self, nums): "&quo ...
- 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 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- MogileFS表说明
MogileFS大致的表说明如下 checksum:用来存放文件的校验和class:文件分类定义device:主机上的可用设备定义,包括设备可用空间,使用的权重等信息domain:域定义信息file: ...
- .net Core CLR
.net Core CLR是开源的.大部分文件是C++写成.这样他就可以编译后再不同的平台运行. https://github.com/dotnet/coreclr
- YII2 实现dropDownList 联动事件
一.视图中 <div class="main-form"> <?php $form = ActiveForm::begin(); ?> <?= $fo ...
- JAVA静态方法是否可以被继承?
结论:java中静态属性和静态方法可以被继承,但是没有被重写(overwrite)而是被隐藏. 原因: 1). 静态方法和属性是属于类的,调用的时候直接通过类名.方法名完成对,不需要继承机制及可以调用 ...
- CentOS7安装CDH 第一章:CentOS7系统安装
相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...
- word生成目录的pdf
在很多情况下,需要将Word转换为带目录书签的PDF,方便pdf阅读,所以可以使用word自带的pdf转换,在转换时设置相关即可 注意:待转换Word中应该有目录,可以用Word中的标题来自动生成目录 ...
- c语言二维数组赋值
, n=;//行数和列数 pattern = (char**)malloc(sizeof(char*)*m);//申请一组一维指针空间. ; i<m; i++) pattern[i] = (ch ...
- 11_Azkaban案例实践4_Command操作MapReduce
MAPREDUCE任务 Mr任务依然可以使用command的job类型来执行 1.创建job描述文件,及mr程序jar包(示例中直接使用hadoop自带的example jar) # mrwc.job ...
- JAVA Calendar类获取上个月的第一天和最后一天
原文:https://www.cnblogs.com/QQParadise/articles/4936313.html 获取上个月第一天的方法: Calendar calendar = Calenda ...
- jquery 选择器能否查找display:none的元素
jQuery可以用可见性“:hidden”查找“display:none”的元素. 1.新家html文档,在head标签中引入本地jQuery文件,也可以引入cdn文件: 2.在body标签中添加一些 ...