LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
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++>的更多相关文章
- [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 ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- 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 ...
- LeetCode 26 Remove Duplicates from Sorted Array
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 【模板】最长公共子序列(LCS)。
看过好多人的博客,感觉要么是太复杂要么就是太不容易理解. 那就亲自动手写一个通俗易懂的. 先定义两个数组,第一个数组为主,用第二个数组来匹配第一个,看能有多少可以对应上的. 所以,其实第一个数组的内容 ...
- (三)ORB特征匹配
ORBSLAM2匹配方法流程 在基于特征点的视觉SLAM系统中,特征匹配是数据关联最重要的方法.特征匹配为后端优化提供初值信息,也为前端提供较好的里程计信息,可见,若特征匹配出现问题,则整个视觉SLA ...
- 【转载】PyTorch系列 (二):pytorch数据读取
原文:https://likewind.top/2019/02/01/Pytorch-dataprocess/ Pytorch系列: PyTorch系列(一) - PyTorch使用总览 PyTorc ...
- ABP架构学习系列四:集成Dapper
之前,一直想集成Dapper到项目中,但是一直没成功,今天把abp升级到最新版,然后按教程来,就可以了,呵呵 现在,基于上一篇的源码进行升级和集成dapper,将abp升级到3.8.2 官方 ...
- .net core WebAPI 初探及连接MySQL
1. 前言 笔者最近跟着微软官方文档学习.net core WebAPI,但发现其对 WebAPI 连接数据库.读取数据库方面讲得不够细致明了.写此文的目的,即实现 .net core WebAPI ...
- Bootstrap-datepicker3官方文档中文翻译---I18N/国际化(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
I18N/国际化 这个插件支持月份和星期名以及weekStart选项的国际化.默认是英语(“en”); 其他有效的译本语言在 js/locales/ 目录中, 只需在插件后包含您想要的地区. 想要添加 ...
- 打造vim IDE
pathogen.vim:vim插件目录自动识别.加载(注意:能用pathogen.vim安装插件,就不要用Vundle.因为Vundle下载插件速度非常慢.) https://github.com/ ...
- Javascript执行上下文和执行栈
什么是执行上下文? 执行上下文就是当前JavaScript代码被解析和执行时所在环境的抽象概念,JavaScript中运行任何的代码都是在执行上下文. 什么是执行栈? 执行栈,在其他编程语言中也被叫做 ...
- 修改MySQL的数据目录
环境:CentOS Linux release 7.1.1503 (Core) 1. 安装MYSQL wget http://dev.mysql.com/get/mysql-community-rel ...
- 集腋成裘-12-git使用-01创建库
一.git安装教程 git安装比较简单,选择好安装路径,直接默认下一步即可 1:检查git是否安装成功 二.SourceTree工具 1:下载&安装 安装过程中如何免注册? 在C:\Users ...