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 ...
随机推荐
- day 18 - 2 正则与 re 模块练习
1.爬虫的例子 #爬虫的例子(方法一) import re import urllib,request import urlopen def getPage(url): response = urlo ...
- LaTex basics
分节: \section{Supplemental Material}\label{sec:supplemental} 小节: \noindent {\bf Preparing References: ...
- Robot Framework学习笔记
robot framework 上个用例的输出作为下个用例的输入 (Set Global Variable的用法) 注意:如果直接在suite里定义变量,变量在suite里的用例里只能应用,修改的效果 ...
- No grammar constraints (DTD or XML Schema) referenced in the document.
问题描述 web.xml 使用 Servlet4.0 版本,No grammar constraints (DTD or XML Schema) referenced in the document. ...
- SVG初尝试(二)
基本图形 rect(矩形).circle.ellipse(椭圆).line(直线).polyline(折线).polygon(多边形).path(可以绘制任意图形) rect x,y定义矩形坐标,矩形 ...
- bat实现固定时间循环抓取设备log
背景:测试时需要实时抓取android设备log,但是一份log抓取过来非常庞大(有时超过500M+,编辑器都打不开,还得找工具进行分割,甚是蛋疼),查看也非常不方便. 解决:基于上述情况,与其之后进 ...
- http 四大特征
- spring-cloud-config-server分布式配置中心
spring cloud config是一个基于http协议的远程配置实现方式.通过统一的配置管理服务器进行配置管理,客户端通过https协议主动的拉取服务的的配置信息,完成配置获取. spring ...
- 防XSS攻击解决方法
1.web.xml文件中新增filter配置 <!-- URL请求参数字符过滤或合法性校验 --> <filter> <filter-name>XssFilter& ...
- php 常用的知识点归集(上)
1.php if...else 在html中的替代语法 <?php $name = 'ok' ?> <?php if ($name == '1ok') : ?> <div ...