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. jetty切换tomcat中文乱码

    项目中文在jetty下正常,换tomcat下出现乱码. 问题是web.xml中的encodingFilter不是第一个,要设置为第一个 <filter> <filter-name&g ...

  2. LeetCode 解题总结

    1. 最长合法括号串 给定只包含'('和')'的字符串,找出最长合法括号串的长度. Example 1: Input: "(()"  Output: 2    Explanatio ...

  3. 三种工具绘制errorbar图

    误差棒是数据可变性的图形表示,并用于图表以指示所报告的测量中的误差或不确定性.他们给出了测量精确度的一般概念,或者相反,距报告值有多远,真实(无误差)值可能是多少.误差线通常代表不确定度的一个标准偏差 ...

  4. JSONP以及端口

    跨域的方式有多种今天我呢,给大家带来的是JSONP接口的操作和接口 JSONP的接口到处都有 今天先拿BOOS直聘的来给大家演示一遍吧 首先找到boss官网:https://www.zhipin.co ...

  5. 二丶CSS

    一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 二.css的 ...

  6. MyBatis异常:元素内容必须由格式正确的字符数据或标记组成

    今天在写接口查询SQL时,报了一个异常,如下: Cause: org.apache.ibatis.builder.BuilderException: Error creating document i ...

  7. 在服务器上实现SSH(Single Stage Headless)

    服务器上ssh实现 写在前面:这只是我在服务器上的环境实现的,仅供参考.要根据自己系统的环境做出修改. ==github源码(https://github.com/mahyarnajibi/SSH)= ...

  8. win下开机不登陆系统自动运行程序的解决方案

    文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作. Jet的电脑可以定时开机了,但是他希望XP系统启动后在不登陆用户的情况下运行锐捷和 ...

  9. youtube去广告

    https://www.digitbin.com/youtube-ads-block/ 1. OGYouTube | Mod AdBlocker YouTube OGYouTube App is a ...

  10. SQL反模式学习笔记3 单纯的树

    2014-10-11 在树形结构中,实例被称为节点.每个节点都有多个子节点与一个父节点. 最上层的节点叫做根(root)节点,它没有父节点. 最底层的没有子节点的节点叫做叶(leaf). 中间的节点简 ...