描述

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,可以考虑distanceunique这两个函数。

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的更多相关文章

  1. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  2. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  3. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  4. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. LeetCode记录之26——Remove Duplicates from Sorted Array

    国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...

  6. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. 【leetcode❤python】26. Remove Duplicates from Sorted Array

    #-*- coding: UTF-8 -*-class Solution(object):    def removeDuplicates(self, nums):        "&quo ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  10. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. ASP.NET Core 过滤器

    继承Attribute,IActionFilter实现自己的过滤器类,并且在Startup,mvc服务中注入. 全局都会过滤,在任意一个controller,action执行前和执行后都会过滤一次 通 ...

  2. 3.ASP.NET Core Docker学习-构建单机多容器环境

    基于docker Docker运行 : docker run -p 8001:80 -d --name name1 name2:1.0 其中-p 8001:80 8001是主机的端口,80是容器的端口 ...

  3. 谷歌浏览器添加插件时显示程序包无效:"CRX_HEADER_INVALID" 解决办法

    今天在添加谷歌插件的时候,却发现谷歌浏览器显示 程序包无效:"CRX_HEADER_INVALID",现整理解决方法如下: 下图是下载好的 .crx 结尾的插件. 将插件的后缀名改 ...

  4. 一步一步写News App(一)

    一. 新建一个安卓工程,安卓版本全部选2.3.3 二.第一步,添加一个tabhost控件 在MainActivity中声明TabHost tabHost; 然后新建一个private void ini ...

  5. Vibe

    在读研和工作之间徘徊了半年,看着一个个好友工作.保研,生活安排得井井有条,我也是时候收拾心情,整装前进了.既然选择了图像,就一定要好好做下去. 今天开始第一个算法,Vibe. ViBe是一种像素级视频 ...

  6. mac安装composer

    推荐阅读:https://www.jianshu.com/p/edde14a67b1a 自己实际操作: 下载 composer https://getcomposer.org/download/ 下载 ...

  7. linux内核过高导致vm打开出错修复脚本

    #!/bin/bashVMWARE_VERSION=workstation-15.1.0TMP_FOLDER=/tmp/patch-vmwarerm -fdr $TMP_FOLDERmkdir -p ...

  8. C++ sizeof(struct) 的注意

    今天在测试将C++代码导出的NavMesh二进制文件用一套C#改写的代码导入时,发现导入的数据出现不一致的问题. 分别在C++和C#AddTile的函数内设置断点,观察最后得到的tile有大部分的字段 ...

  9. wepy全局拦截器

    wepy有支持全局拦截器,但是请求需要使用wepy.request().then(): 在app.wpy文件中配置以下内容,与data同级 constructor(){ super(); this.u ...

  10. day12-python之深灰魔法

    #######################################灰魔法: list类中提供的方法   列表####################################### ...