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. P1052 过河

    动态规划的好题 状态转移很简单,dp[i] = dp[i-k] + st[i] ,k是移动距离,st[i]判断i位置是否有石头,但是距离太大,需要压缩路径. K∈[1,10],lcm[1,10] = ...

  2. ESLint学习小记

    一.关于配置文件,优先级从上到下: eslintrc.js .eslintrc.yaml .eslintrc.yml .eslintrc.json .eslintrc package.json 在官方 ...

  3. MVC 前端页面ViewData参数名不区分大小写

    项目中实际应用: 后台赋值时传的是:ViewData["CheckedSystemMenu"], 前台取值时:ViewData["checkedsystemmenu&qu ...

  4. Pompholyx - Causes, Symptoms and Treatment

    Pompholyx is a common type of eczema. It is also known as dyshidrotic eczema or vesicular eczema of ...

  5. Android 8.0 无法收到broadcast

    参见此页 https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html 目前最简单的方法就是:把targe ...

  6. swoole异步群发模板消息

    1.用的是TP5.1的框架,swoole分成一个客户端发送接收消息,一个服务器负责处理信息 服务端代码,服务器要先安装swoole拓展,用 php server.php 启动进程监听 <?php ...

  7. gitignore规则探究

    PS:转自https://blog.csdn.net/o07sai/article/details/81043474 网上有好多gitignore的帖子,文章,都说很简单的.但是我怎么就用不好呢? 我 ...

  8. vue -- style使用scss样式报错

    1.报错信息 2.解决方案(vue-li默认没有scss-loader,scss-loader)安装以下依赖 (1) npm install node-sass --save (2)npm insta ...

  9. [转] 使用Node.js实现简易MVC框架

    在使用Node.js搭建静态资源服务器一文中我们完成了服务器对静态资源请求的处理,但并未涉及动态请求,目前还无法根据客户端发出的不同请求而返回个性化的内容.单靠静态资源岂能撑得起这些复杂的网站应用,本 ...

  10. [转] JS中的call()方法和apply()方法用法总结

    //例1 <script> window.color = 'red'; document.color = 'yellow'; var s1 = {color: 'blue' }; func ...