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. PHP程序运行性能分析

    php在使用了xdebug后,可以配置xdebug相关的配置,生成运行的日志. 在php.ini中配置: xdebug.profiler_enable = 1 xdebug.profiler_enab ...

  2. day 22 - 2 面向对象练习

    练习一 在终端输出如下信息 小明,10岁,男,上山去砍柴小明,10岁,男,开车去东北小明,10岁,男,最爱大保健老李,90岁,男,上山去砍柴老李,90岁,男,开车去东北老李,90岁,男,最爱大保健老张 ...

  3. JAVA进阶16

    间歇性混吃等死,持续性踌躇满志系列-------------第16天 1.桌球游戏小项目 ①窗口加载 import javax.swing.*; public class BallGame exten ...

  4. 开源mall学习

    https://github.com/macrozheng/mall 学习知识点 1.Spring Security 2.@Aspect 3.logstash 4. es crud templete ...

  5. Windows安装docker (带安装包)

    docker安装包链接 链接:https://pan.baidu.com/s/1JBk8GCH6j_WeGdoaUuIoWw 提取码:8kgg 我电脑上有了git所以没有勾选最后一个 安装完成后将此目 ...

  6. MailKit系列之转发电子邮件

    原文:http://www.it1352.com/429181.html 问题 我尝试通过MailKit访问一个IMAP账号,我设法下载邮件(作为的MimeMessage),并在某些时候我需要转发给其 ...

  7. python学习之re (?P...)通过关键字获取组以及( P=name)

    和其他的RE表达式一样,但是匹配的子串可以通过group的名字 name来获取.即  result.group('name')  (提示,字符串数字都是常量,所以关键字都可以被视为整型(hash结果) ...

  8. 初学python之路-day08前期总结

    # 1# 计算机原理:控制器 运算器 存储器 input设备 output设备 IO流# 三大核心:CPU 内存 硬盘 # 内存分布:栈区 与 堆区# 如二进制与十进制的转换,如1111转成十进制为1 ...

  9. Chrome浏览器自动填充<input>标签的密码

    问题:登录页面登录时,Chrome浏览器保存了用户名和密码,在其他页面管理其他的账户和密码时,密码框先是显示正确的密码,然后一闪而过被覆盖. 原因:问了技术主管才得知,Chrome浏览器中的,保存用户 ...

  10. spring boot正常启动但是访问会找不到“ localhost 的网页”的错误

    最近启动springboot项目访问localhost老报找不到网页,找了很久发现yml配置文件中配置了“context.path”,只要在端口号后面加上context.path地址就可以访问了. 如 ...