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. flask学习(二)

    一.蓝图 作用:给开发者提供目录结构 功能:1.自定义模板.静态文件目录 2.给一类url加前缀    3.给一类url添加before_request 目录结构 from flask_werkzur ...

  2. git知识总结-2.git基本操作之操作汇总

    0.前言 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 上图分别为: Workspace:工作区 Index / Stage:暂存区 Reposito ...

  3. ajax上传文件显示进度

    下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...

  4. js-图片预加载

      //图片预加载 //闭包模拟局部作用于 (function($){ function Preload(imgs,options){ this.imgs = (typeof imgs === 'st ...

  5. 出错:Error creating bean with name 'studentServiceImpl': Unsatisfied dependency expressed through field 'studentMapper';

    详细错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam ...

  6. 中位数——二维坐标下的中位数lightoj1349

    第一次碰到这种题,不知所措,题解链接 => https://www.cnblogs.com/fu3638/p/7426074.html #include<bits/stdc++.h> ...

  7. U盘安装Mac OS X要点

    1.启动U盘必须比系统磁盘小,因为制作启动U盘,U盘很可能被当成系统盘.系统安装时,发现系统盘比U盘小,很可能提示安装失败. 2.制作启动U盘.详情查看官网:https://support.apple ...

  8. linux extglob模式 和rm反选

    前言 extglob模式开启之后Shell可以另外识别出5个模式匹配操作符,能使文件匹配更加方便. 不然不识别! 正文 #开启命令: shopt -s extglob #关闭命令: shopt -u ...

  9. sql查找某一列中某一数值出现次数大于3的记录的前3条

    SELECT * FROM table  GROUP BY column HAVING COUNT(column)>=3 ORDER BY column DESC LIMIT 0,3;

  10. 用 pdf.js兼容部分安卓显示PDF在线预览 时,a标签直接链接参数文件不能含中文的解决办法

    例子: 项目部署在 Tomcat 上的: <a href="../generic/web/viewer.html?file=doc/register/要显示的文件.pdf" ...