一天一道LeetCode系列

(一)题目

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 nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

(二)解题


/*

解题:排好序的数据,删除里面重复的数据

需要注意以下两点:

1.erase()调用之后迭代器失效,需要将iter = nums.erase(iter);

2.考虑nums为空或者只有1个的情况,可以直接返回

*/

class Solution {

public:

    int removeDuplicates(vector<int>& nums) {

        if(nums.size()<=1) return nums.size();

        auto iter = nums.begin() +1;

        for(;iter!=nums.end();)

        {

            if(*iter == *(iter-1))

            {

                iter = nums.erase(iter);//关键!erase()返回的是删除的数据的下一个迭代器

            }

            else

                ++iter;//没有删除元素的时候+1

        }

        return nums.size();

    }

};

【一天一道LeetCode】#26. Remove Duplicates from Sorted Array的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

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

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  3. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

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

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

  5. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

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

  6. [LeetCode] 26. Remove Duplicates from Sorted Array ☆

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

  7. LeetCode 26 Remove Duplicates from Sorted Array

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

  8. Java [leetcode 26]Remove Duplicates from Sorted Array

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

  9. Leetcode 26. Remove Duplicates from Sorted Array (easy)

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

  10. [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

随机推荐

  1. iOS开发基础之设置状态栏和二维码的unspported type found 问题

    最近遇到设置状态栏和二维码AVCaptureMetadataOutput setMetadataObjectTypes: unspported type found的错误问题,所以我在这里进行总结一下 ...

  2. GitLab服务器IP地址设置

    最近使用GitLab 搭建了Git的私有仓库,但是发现私有仓库的地址居然是localhost,不是本机的IP地址,最后百度了一下,找了很久才找到,特此记录一下. 首先说明一下,我linux虚拟机的IP ...

  3. Mac小技巧:快速查看指定应用程序的所有窗口

    我们知道在Mac中快速在系统所有程序中切换得快捷键为: cmd + tab 不过有时我们需要快速查看某一个程序的所有窗口,那又该如何呢? 以下方法在MacOS 10.12中测试成功! Mac默认该功能 ...

  4. Spring+EhCache缓存实例(详细讲解+源码下载)

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

  5. oracle伪列

    Oracle的伪列以及伪表 oracle系统为了实现完整的关系数据库功能,系统专门提供了一组成为伪列(Pseudocolumn)的数据库列,这些列不是在建立对象时由我们完成的,而是在我们建立时由Ora ...

  6. JAVA面向对象-----extends关键字

    继承使用extends关键字实现 1:发现学生是人,工人是人.显然属于is a 的关系,is a就是继承. 2:谁继承谁? 学生继承人,发现学生里的成员变量,姓名和年龄,人里边也都进行了定义.有重 复 ...

  7. Java进阶(三十八)快速排序

    Java进阶(三十八)快速排序 前言 有没有既不浪费空间又可以快一点的排序算法呢?那就是"快速排序"啦!光听这个名字是不是就觉得很高端呢. 假设我们现在对"6 1 2 7 ...

  8. [ExtJS5学习笔记]第四节 欢迎来到extjs5-手把手教你实现你的第一个应用

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38331347 本文作者:sushengmiyan ------------------ ...

  9. Android初级教程图片信息

    对图片常规信息要了解其性质.图片大小.像素.位图等等概念总结如下: 图片在计算机中的大小 图片的总大小 = 图片的总像素 * 每个像素占用的大小(图片的总像素=像素尺寸也就是分辨率,例如设定800*4 ...

  10. JQuery纵向下拉菜单实现心得

    jquery库给我们带来了许多便利,不愧是轻量级的DOM框架,在前面的博文中小编分别对jquery的基础知识以及jquery的一些小demo有一系列的简单介绍,期待各位小伙伴的指导.使用jquery实 ...