【Leetcode】Remove Duplicates from Sorted Array II
题目:对上一题的延伸,每个数字可以出去2次。
思路:还是设置两个下标。第一个lenxb标记已去重的地方,第二个i标记待处理的位置。每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出现超过两次了;否则满足要求。
注意:通过上面的思路可知,特判情况是长度小于等于2时。
PS:提交后16ms,在此题的提交时间第一队列最前位置。Yes!
代码:
class Solution { public:int removeDuplicates(vector<int>& nums) {if(nums.size()<=2) return nums.size();int lenxb=1;//长度的下标for(int i=2;i<nums.size();++i){if(nums[i]==nums[lenxb] && nums[i]==nums[lenxb-1]) continue;nums[++lenxb]=nums[i];}return lenxb+1;} };
转载于:https://www.cnblogs.com/buxizhizhou/p/4684490.html
【Leetcode】Remove Duplicates from Sorted Array II相关推荐
- 【leetcode】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 【Leetcode】Remove Duplicates from Sorted List II
问题: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- LeetCode 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...
- LeetCode之Remove Duplicates from Sorted Array II
1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- 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++> 给出排序好的 ...
- Leetcode OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
最新文章
- python中的连续比较是什么_在python中提取连续行之间的差异
- 贴吧html标签,html标签3(转载)
- bool类型未初始化的产生的奇怪现象
- Linux操作系统安装LAMP环境
- il和idl区别_我来给普及一下matlab、IDL、fortran神马的区别吧
- JVM内存模型分析(一个程序运行的例子)
- 《RHEL6.3 FTP服务器虚拟用户的配置(含图)》——如此简单
- 2018年python工作好找吗-2018年最新数据:python、大数据、人工智能从业者薪资表...
- 【SSM】手动SQL分页和PageHelper插件分页
- tungsten mysql_ETL之Tungsten Replicator
- 计算机专业的英语六级很难过,英语六级多少分算过 通过率高吗
- RGB-D SLAM in Dynamic Environments Using Static Point Weighting论文笔记
- 国外大牛推荐:计算机专业人士必读好书(30本经典)
- 机器学习 Python scikit-learn 中文文档(10)Scikit-Learn 处理文本数据
- *Error* illegal LHS in continous assignment
- PCL——点云特征描述与提取
- java棒棒糖和皮卡丘八音盒_如何比别人抢先一步买到肯德基的皮卡丘八音盒?我有两个小建议...
- 思科认证入门级课程介绍(一)
- lotus domino_保护IBM Lotus Domino Web服务器的安全:案例研究
- MySQL查询时间戳
热门文章
- 压电式加速计matlab,低频压电加速度传感器的噪声特性及信号处理方法研究
- 地铁系统_北斗授时助力北京地铁地下定位系统
- java 示例_Java最终关键字示例
- java匿名类_Java匿名类
- android圆角视图_Android图库视图示例教程
- scala 类中的对象是类_Scala类和对象– Singleton对象,伴侣类
- 开课吧课堂之未被捕获的异常
- ​七周二次课(5月7日)监控io性能、free命令、ps命令、查看网络状态、linux下抓包...
- 手把手教你如何把本地文件传到服务器,如何映射
- CentOS利用LVM实现磁盘弹性扩容