LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
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++>的更多相关文章
- [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% ...
- [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 ...
- [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 ...
- [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 ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...
随机推荐
- 分布式系列十二: Redis高级主题
持久化 Redis 支持持久化, 其持久化数据有两种方式. 两种可以同时使用. 如果同时使用, Reids 在重启时将使用 AOF 方式来还原数据. RDB 按照一定策略定时同步内存的数据到磁盘.文件 ...
- Mac环境下Redis的安装
1.下载 官网下载地址:https://redis.io/download,选择对应的下载版本,我下载的是4.0.12 2.安装 1)下载文件解压后复制到/usr/local/目录下(快速找到路径小技 ...
- SQL总结 连表查询
连接查询包括合并.内连接.外连接和交叉连接,如果涉及多表查询,了解这些连接的特点很重要. 只有真正了解它们之间的区别,才能正确使用. 1.Union UNION 操作符用于合并两个或多个 SELECT ...
- delphi
其实现在哪里还有delphi,我之所以建立这个分类,只是为了纪念它,纪念我使用了一年delphi的经历. 那个年代c#刚出道,delphi还是c/s编程届数一数二的人物.但是没想到,这么快被c#打败了 ...
- 使用 eclipse 的常用操作
1.创建项目 https://blog.csdn.net/tsundere_ning/article/details/79587060 2. 常用代码块创建编辑 使得eclipse 相应, 点击右上角 ...
- 团队软件的NABCD——星遇
日期:2019.4.17 博客期:053 星期三 我们项目是个面向希望有新奇体验的用户的社交软件,致力于打造不一样的有趣的社交. N:(Need,需求) 目前主流社交软件由于时间原因体量越来越大,各种 ...
- ogma
Ogma是Linkurious的JavaScript图形可视化库.Ogma的一个实例是一个javascript对象,它在内部存储一个图形, 并根据样式规则在HTML容器中呈现它. Ogma有两个版本: ...
- ffmypeg 视频处理类库使用方法
(经常用到ffmpeg 做一些视频数据的处理转换等,用来做测试,今天总结了一下,参考了网上部分朋友的经验,一起在这里汇总了一下,有需要的朋友可以收藏测试一下,有问题欢迎在下面回帖交流,谢谢;by te ...
- 点击编辑table变为可编辑状态
简单描述:开发中遇到一个小困难,table展示的数据,需要是可编辑的,点击编辑按钮,页面table可以进行编辑,编辑完成后,点击保存数据就保留下来~~~ 思路:用一个带有input的表去替换不带inp ...
- linux 相关( 随时更新)
Linux笔记: 本地文件传到服务器上: scp -P8022 /文件路径 niehaidong@101.201.75.57:/tmp/ 从服务器上到本地 scp -P8022 niehaidong ...