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. ssh远程访问失败 Centos7

    ssh远程访问失败 Centos7 命令ssh远程访问另外一台主机hadoop.master失败 #ssh hadoop.master 报以下信息 [root@hadoop ~]# ssh hadoo ...

  2. re模块 - 正则表达式 疏理(一)

    在网上总是很难找到令自己比较满意的,关于正则表达式的文章.所以决定自己来总结一波,并配上相应的示例. 正则表达式:定义了规则,用来字符串处理. 用途: 1.匹配 - 符合规则的字符串,则认为匹配了. ...

  3. LaTeX多图合并代码示例(subfigure)

    \usepakage{subfig} \begin{figure*}[!htb] \centering \subfigure[Derm101 data distribution]{\includegr ...

  4. Visual Studio 2013 SDK 扩展之简介

    Release Notes:[发行说明]启动记事本的扩展,以管理员身份运行验证通过. Getting Started Guide:[入门]使用[Ctrl + 1]更快捷打开记事本 More Info ...

  5. go语言实现生产者-消费者

    前言: 之前在学习操作系统的时候,就知道生产者-消费者,但是概念是模模糊糊的,好像是一直没搞明白. 其实很简单嘛,生产者生产,消费者进行消费,就是如此简单.了解了一下go语言的goroute,感觉实现 ...

  6. 主席树套树状数组——带修区间第k大zoj2112

    主席树带修第k大 https://www.cnblogs.com/Empress/p/4659824.html 讲的非常好的博客 首先按静态第k大建立起一组权值线段树(主席树) 然后现在要将第i个值从 ...

  7. How does rt.jar works?

    转载自:https://stackoverflow.com/questions/30222702/how-does-java-link-lib-rt-jar-to-your-app-at-runtim ...

  8. 【UER #8】雪灾与外卖

    题解: 这个东西的模型是个费用流 但是直接跑费用流能拿到5分的高分 $(nm)*(nm)*log{nm}$ 考虑优化一下建图 我们可以不用对每个店和人都连边 而是对人和店都连一条链 然后对每个人连店刚 ...

  9. 分支界定( BRANCH-AND-BOUND)

    分支定界法(branch and bound)是一种求解整数规划问题的最常用算法.这种方法不但可以求解纯整数规划,还可以求解混合整数规划问题.分支定界法是一种搜索与迭代的方法,选择不同的分支变量和子问 ...

  10. Python学习(四十二)—— Djago-model进阶

    一.QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. Entry.objects.all()[:5] # (LIMI ...