LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间。

C++

献上自己丑陋无比的代码。相当于自己实现一个带计数器的unique函数

class Solution {
public:
int removeDuplicates(std::vector<int>& nums) {
if(nums.empty())
return 0;
int cnt = 0;
auto slow = nums.begin();
auto last = *nums.begin();
for(auto fast:nums){
if(cnt == 0) cnt++,slow++;
else if(cnt == 1){
if(fast == last) cnt++;
*slow = fast;
slow++;
}
else {
if(fast != last) {
cnt = 1;
*slow = fast;
slow++;
}
}
last = fast;
}
return distance(nums.begin(),slow);
}
};

学习标程后写的更简洁的版本,这样的代码扩展性更好:

class Solution {
public:
int removeDuplicates(std::vector<int>& nums) {
if(nums.size()<=2) return nums.size();
auto index = nums.begin()+2;
for(auto i = nums.begin()+2;i!=nums.end();i++){
if(*i!=*(index-2)) *index++ = *i;
}
return std::distance(nums.begin(),index);
}
};

再贴一份wiki上找来的std::unique的使用示例。感受一下vector也是可以方便的初始化的。 还有auto的使用(基本操作)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype> int main()
{
// remove duplicate elements
std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
std::sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7
auto last = std::unique(v.begin(), v.end());
// v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
v.erase(last, v.end());
for (int i : v)
std::cout << i << " ";
std::cout << "\n";
}

Java

Python3

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>的更多相关文章

  1. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  2. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  3. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  6. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

  8. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)

    题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...

随机推荐

  1. docker安装redis

    查询镜像 docker search redis 拉取镜像 docker pull redis 启动容器 docker run --name redis -p 6379:6379 -d --resta ...

  2. Could not find a package configuration file provided by "Qt5Widgets"

    解决: sudo apt install qttools5-dev

  3. BH1750FVI调试

    在写此博客之前已经看了几遍数据手册了,现在已经调试成功了,可以读出来数据,还有不如意的地方,写此博客整理下思路. 1.BH1750fvi介绍. 这是一个16bit的数字传感器,使用I2C作为通信接口, ...

  4. 阻塞IO,非阻塞IO,IO多路复用模型

    #服务端 import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() while True: conn, ad ...

  5. CentOS 安装 Docker

    前言:其实安装步骤Docker官网很详细,如果有些人英文不好看的比较慢的话就可以直接看我的,我也是摘自官网,具体步骤如下 1. 安装依赖包 $ sudo yum install -y yum-util ...

  6. SQLAlchemy+Flask-RESTful使用(二)

    前言 本来没想到能这么快出二的,谁知道序列化组件写上头了.分享知识真的会上瘾.... 变更记录 # 19.3.18 起笔 # 19.3.18 使用SQLAlchemy排序方法 # 19.3.18 补充 ...

  7. nginx rewrite规则笔记

    优先级 在nginx的location和配置中location的顺序没有太大关系.正location表达式的类型有关.相同类型的表达式,字符串长的会优先匹配. 第一优先级:等号类型(=)的优先级最高. ...

  8. 2小程序canvas使用,及一些坑,以及自己的一些小总结

    自己做了一个小程序,主要用于给头像加图标的那种,和qq似的,主要用canvas做的, 第一回用,掉了很多坑,所以今天总结一下自己所做的,如果大家有不理解的地方,欢迎提问:如果帮到大家的话,帮忙点个啥的 ...

  9. 如何给PDF文件制作书签

    书本阅读的时候我们有时候会制作一些漂亮的书签,那么电子文档也是有书签的,要怎么制作小伙伴们都知道吗?应该会有许多的小伙伴还不知道,今天就为大家分享一下电子文件如何添加书签的.就以PDF这个现在常用的电 ...

  10. javascript中new Date()存在的兼容性问题

    问题:通过new Date()创建的时间对象在Chrome能正常工作,但在IE浏览器却显示NaN 代码: var time = new Date(date + ' 00:00:00'); //NaN ...