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 ...
随机推荐
- 关于arm 的字节对齐
一.什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问,这 ...
- (五)Struts之Action类基础(二)
上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...
- (错误) Eclipse使用Maven创建Web时错误
转自:http://blog.csdn.net/afgasdg/article/details/12757433 问题描述: 使用Eclipse自带的Maven插件创建Web项目时报错: Could ...
- Thymeleaf 模板使用 Error resolving template "/home", template might not exist or might not be accessible by any of the
和属性文件中thymeleaf模板的配置相关 1.配置信息 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix= ...
- java8新特性的介绍
什么是Stream Stream是一个来自数据源的元素队列并可以进行聚合操作. 数据源:流的来源. 可以是集合,数组,I/O channel, 产生器generator 等 聚合操作:类似SQL语 ...
- 【SQL Server性能优化】运用SQL Server的全文检索来提高模糊匹配的效率
原文:[SQL Server性能优化]运用SQL Server的全文检索来提高模糊匹配的效率 今天去面试,这个公司的业务需要模糊查询数据,之前他们通过mongodb来存储数据,但他们说会有丢数据的问题 ...
- Html5+Mui前端框架,开发记录(四):下拉菜单绑定数据、搜索、时间控件
1.下拉菜单绑定数据,选择后回传值 1)html: <div class="mui-input-row"> <label>xxx:</label> ...
- C#通过重载构造函数传递参数、实现两个窗体下的方法的互相调用
直接切入主题 有时候同一个项目下我们可能会使用多个窗体,窗体间方法互相调用也不可避免,好了,使用无参无返回值的方法,开始上图 1.新建一个winform项目Form1,并再添加一个窗体Form2:拖入 ...
- sudo pip3找不到命令
转自: https://blog.csdn.net/Cryhelyxx/article/details/53384004 编辑/etc/sudoers 找到Defaults env_reset, 将其 ...
- JS中this和call
首先来了解一下JS中this的原理: 要访问自己的属性就必须使用 this.属性名 1.this总是指向它的直接调用者: var a={ user:'Artimis', fn:function(){ ...