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

给出排序好的一维数组,删除其中重复元素,返回删除后数组长度,要求不另开内存空间。

C++

很简单的题目,但是第一发RE了,找了很久问题出在哪。最后单步调试发现vector.size()返回值是unsigned型的。unsigned型和int型数据运算结果还是unsigned型的。这就导致当数组为空时,nums.size()-1是一个大正整数,循环变量i很大时,执行了越界下标访问,出现了段错误。

/*
Status: Runtime Error
Runtime Error Message:
Line 933: Char 34: runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h)
Last executed input:
[]
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
unique(nums.begin(),nums.end());
for(unsigned i = 0; i < nums.size()-1; ++i) {
if(nums[i]>=nums[i+1])return i+1;
}
return nums.size();
}
};

虽然知道这个题用STL可以很方便,但是看到函数主体只有一行的代码时我还是感觉自己too young

/*
Status: Accepted
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return distance(nums.begin(),unique(nums.begin(),nums.end()));
}
};

然后去学了一发std::distance的用法:就是返回两个迭代器之间的距离。

因为std::unique返回最后一个不重复元素的迭代器,所以首迭代器与最后一个不重复元素的迭代器的距离就是不重复元素的个数,即数组长度。

Java

Python3

LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>的更多相关文章

  1. [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 ...

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

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

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

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

  4. 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 ...

  5. LeetCode 26 Remove Duplicates from Sorted Array

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

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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. Excel如何快速统计一列中相同数值出现的个数--数据透视表

    excel如何快速统计一列中相同数值出现的个数_百度经验 --这里介绍了两种解决方式,用第一种https://jingyan.baidu.com/article/9113f81b2c16822b321 ...

  2. java界面--WePush-master 项目跑起来 -碰到的问题

    仅能在IntelliJ里面打开GUI Form文件来进行界面的拖拽设计--eclipse中也可以了 当eclipse 打开.form文件时,提示有.对应的编辑工具可以打开它,请到市场去安装 但是:本人 ...

  3. oracle ORA-02292: 违反完整约束条件

    我是处于工作中没用过oracle的状态,这不,记录下这个小小的问题.哈哈. 表是公司的平台组定义的.前几天为了测试程序,想删掉一些记录,然后使用delete语句,出现这个东东:oracle ORA-0 ...

  4. 2018-2019-2 20165231《网络对抗技术》Exp0 Kali安装 Week1

    下载Kali Linux系统 进入官网进入下载页面,因为我们是在虚拟机内使用,而官网已经为我们提供了VM版的所以我就直接下载了这个版本的. 根据官网提示使用管理员帐号root(密码为toor)登录,创 ...

  5. Git管理源代码

    Git Git 是目前世界上最先进的分布式版本控制系统(没有之一) 作用 源代码管理 为什么要进行源代码管理? 方便多人协同开发 方便版本控制 Git单人本地仓库操作 安装git sudo apt-g ...

  6. A previous installation of Qt5 Visual Studio Add-in was detected. Please uninstall it before running this installer解决办法

    前段时间在安装Qt Visual Studio插件的时候,安装到一半不小心中止了,结果后来怎么安装都不行,提示已经安装了,要先卸载, 可是到哪里都找不到有卸载的,因为压根就没有安装完成.这可害苦我了. ...

  7. notepad++安装nppFTP

    官网下载的最新版notepad++,结果pluginadmin里面installnppftp总是安不上,点击install之后然后点是就退出,再进去也没有安装好. 网上找了半天也没找到什么有用信息,最 ...

  8. Laravel-google-authenticator--Google验证码

    开发前的准备 安装Laravel 安装二维码生成器QrCode,没有安装也可以,接下来会安装 安装拓展 1.运行如下代码安装拓展包: composer require "earnp/lara ...

  9. W3CSchool实战闯关笔记(JavaScript)

    //handsome /** *ugly **/ 第一关注释 // 举例 var myName; // Define myName below this line 第二关声明变量 // Setup v ...

  10. java http post/get 服务端和客户端实现json传输

    注:本文来源于<java http post/get 服务端和客户端实现json传输> 最近需要写http post接口所以学习下. 总的还是不难直接上源码! PostHttpClient ...