描述

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. Redis主从及Cluster区别及注意事项

    https://yq.aliyun.com/articles/647342 https://blog.csdn.net/biren_wang/article/details/78117392 http ...

  2. Skip List(跳跃表)原理详解与实现

    ref : https://dsqiu.iteye.com/blog/1705530   本文内容框架: §1 Skip List 介绍 §2 Skip List 定义以及构造步骤   §3 Skip ...

  3. 在oracle表中增加、修改、删除字段,表的重命名,字段顺序调整

    增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...

  4. Codeforces 1245 D. Shichikuji and Power Grid

    传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...

  5. Bloom过滤器

    提出一个问题 在我们细述Bloom过滤器之前,我们先抛出一个问题:给你一个巨大的数据集(百万级.亿级......),怎么判断一个元素是否在此数据集中?或者怎么判断一个元素不在此数据集中? 思考这个问题 ...

  6. 【爬虫集合】Python爬虫

    一.爬虫学习教程 1. https://www.jianshu.com/u/c32d557edfa3 2. WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个 ...

  7. 监控SQL:通过SQL Server的DDL触发器来监控数据库结构的变化(1)

    原文:监控SQL:通过SQL Server的DDL触发器来监控数据库结构的变化(1) 如果你要同步不同数据库之间的数据,首先会想到的是数据库复制技术,但如果让你同步数据库的结构,你会想到什么呢? 下面 ...

  8. .NetCore如何使用ImageSharp进行图片的生成

    ImageSharp是对NetCore平台扩展的一个图像处理方案,以往网上的案例多以生成文字及画出简单图形.验证码等方式进行探讨和实践. 今天我分享一下所在公司项目的实际应用案例,导出微信二维码图片, ...

  9. Window环境下使用多个Git账号(github,gitee,gitlab,gogs等)

    个人电脑之前已经设置好github账号了,公司用的是gitlab私服,一直互不干扰,因为用的是不同的电脑,也就懒得配置git多账户环境.最近看了一下多年空空如也的码云,想着怎么的也会用到gitee来托 ...

  10. 第三代PacBio测序技术的测序原理和读长

    针对PacBio单分子测序——第三代测序技术的测序原理和读长     DNA基因测序技术从上世纪70年代起,历经三代技术后,目前已发展成为一项相对成熟的生物产业.测序技术的应用也扩展到了生物.医学.制 ...