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

转载于:https://www.cnblogs.com/NeilThang/p/10304281.html

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, remove the duplicates in place such that each element appear only once and ...

  2. leetCode #26 Remove Duplicates from Sorted Array

    删除相同数字 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int coun ...

  3. [leetcode]83.Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  4. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  5. [勇者闯LeetCode] 83. Remove Duplicates from Sorted List

    [勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...

  6. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

  7. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

  8. LeetCode之Remove Duplicates from Sorted Array II

    1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  9. leetcode:2680 Remove Duplicates from Sorted Array 删除数组中的重复元素

    leetcode:26 对数组元素进行去重,使得原数组重复元素最多保留1个 限制: 我们不可以额外分配数组,必须保持空间复杂度为O(1) 这个并不难实现: class Solution(object) ...

最新文章

  1. vim 批量注释代码
  2. C#中new和override的区别
  3. ASP.NET Core amp; Docker 实战经验分享
  4. Gym100917 A - Abstract Picture
  5. console 速查手册
  6. Eureka心跳续约机制
  7. ubuntu16.04 内核源码编译
  8. golang gin解决跨域:编写一个全局中间件
  9. dependencyManagement使用简介
  10. python实现可视化数独求解器(附代码链接及点点讲解)
  11. 教你如何真正玩转XP共享
  12. Excel 合并当前工作簿全部工作表的VBA代码
  13. VMware16虚拟机:安装Windows10系统---超详细教程
  14. 2022秋招笔试备考合集——银行篇(下)|智测优聘出品
  15. Deep Closest Point学习笔记(才开始接触点云)
  16. 〖Python语法进阶篇⑱〗- 综合实战 - 抽奖系统之admin模块 - 抽奖系统之admin模块 - 实现对奖品的增删改操作
  17. 第三章习题3第4题--for循环输出俄文字母表
  18. Python学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
  19. 如何在华为云软件开发云上运行Python
  20. 数据库系统与文件系统的区别

热门文章

  1. 《构建之法》阅读笔记2
  2. HDOJ2795 Billboard【线段树】
  3. WP7之题样式与数据绑定
  4. 以系统最高权限运行软件
  5. MyEclipse中jsp编码设置
  6. c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
  7. 基于CentOS 搭建 Seafile 专属网盘
  8. 实战c++中的vector系列--vectorlt;unique_ptrlt;gt;gt;初始化(全部权转移)
  9. from表单POST提交nodejs
  10. iOS内存管理编程指南