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 ...
随机推荐
- 解决GitHub下载速度缓慢的问题
随着微软大大宣布GitHub针对个人用户的仓库免费,相信每位开发者都感受到了"真香". 然而因为一些众所周知的原因,国内访问GitHub总会遇到下载速度缓慢.链接意外终止的情况. ...
- CAS 5.x搭建常见问题系列(3).Failure to find org.apereo.cas:cas-server-support-pm-jdbc:jar:5.1.9
错误内容 cas overlay的pom.xml增加了cas-server-support-pm-jdbc.jary依赖后, 打包(mvn package)出现如下的报错 D:\casoverlay\ ...
- 菜鸡之NetCore 使用EF操作数据库 Oracle & Sqlserver (一)
摘要: 该篇文章主要记录netCore EFCore 如何操作Oracle和SqlServer 数据库,采用Codefirst方式创建数据库以及表. 一, 项目建立 项目采用DDD领域驱动设计模式[学 ...
- [转载]JDK、SDK、J2EE、J2SE、J2ME的区别
[转载]JDK.SDK.J2EE.J2SE.J2ME的区别 来源: https://www.cnblogs.com/liangyihui/p/5905875.html Java的名词真的是多啊 JDK ...
- ipp 实现图像空间的转换
下载:https://software.intel.com/en-us/parallel-studio-xe/choose-download/free-trial-cluster-windows-c- ...
- ConfigParser读取配置文件时报错:ConfigParser.MissingSectionHeaderError
使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...
- 使用apache的HttpClient进行http通讯,隐藏的HTTP请求头部字段是如何自动被添加的
我们用apache的HttpClient这个库消费云端的Restful API时,一般都需要两次HTTP调用,第一次获得某种token,比如获取防止跨域请求伪造攻击Cross-site request ...
- 超详细的Java面试题总结之JavaWeb基础知识总结
ervlet总结: 在Java Web程序中,Servlet主要负责接收用户请求HttpServletRequest,在doGet(),doPost()中做相应的处理,并将回应HttpServletR ...
- Linux磁盘管理——swap分区
转自:Linux Swap交换分区设置 对swap分区的误解 一种流行的.以讹传讹的说法是,安装Linux系统时,交换分区swap的大小应该是内存的两倍.也就是说,如果内存是2G,那么就应该分出4G的 ...
- CSS浮动特性
float:left/right左浮动有浮动 特点: ①浮动不占位:浮动元素不占位置 ②默认排列成一行,遇到边界自动换行 ③如果有文字(没有设置浮动的元素内容)会绕着浮动元素走 <!DOCTYP ...