删除重复值(2以上)Remove Duplicates from Sorted Array II
2019独角兽企业重金招聘Python工程师标准>>>
问题:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3]
,
Your function should return length = 5
, with the first five elements of nums being 1
, 1
, 2
, 2
and 3
. It doesn't matter what you leave beyond the new length.
解决:
① 只需要维护一个count,当count是2时,就直接跳过即可,否则说明元素出现次数没有超,继续放入结果数组,若遇到新元素则重置counter。
class Solution {//1ms
public int removeDuplicates(int[] nums) {
if(nums.length <= 2) return nums.length;
int index = 0;
int count = 0;
for (int i = 0;i < nums.length;i ++) {
if(i >0 && nums[i] == nums[i - 1]){
count ++;
if(count >= 3){
continue;
}
}else{
count = 1;
}
nums[index ++] = nums[i];
}
return index;
}
}
转载于:https://my.oschina.net/liyurong/blog/1538629
删除重复值(2以上)Remove Duplicates from Sorted Array II相关推荐
- 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 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II
重复一次 [抄题]: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. [思维问题]: [ ...
- lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
题目: 删除排序数组中的重复数字 II 跟进"删除重复数字": 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- [LeetCode] Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- LeetCode之Remove Duplicates from Sorted Array II
1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
最新文章
- Python生物信息学①将RNA序列翻译成蛋白质序列。
- 美国微生物科学院22年院士公布!舒跃龙、黄力、卢洪洲、赵国屏!
- 零代码如何打造自己的实时监控预警系统
- HealthKit开发快速入门教程之HealthKit框架体系创建健康AppID
- Spring集成TestNg测试
- 约瑟夫问题pascal程序
- 深入理解Spark 2.1 Core (十四):securityManager 类源码分析
- CAE所表示的计算机术语是,计算机应用中,英文缩略语CAE所表示的计算机术语是()。...
- 为什么有些女孩在发现渣男的真面目以后,还喜欢他们?
- 资源地址整合与备份,即时更新
- 利用python进行数据分析——第十四章_数据分析案例
- 【转】ThinkPHP命令行工具Tptool2.0使用教程
- 学习:大文件统计与排序
- SadpTool 海康设备网络搜索工具
- Tracepro中up vector和normal vector的定义2
- php 根据身份证计算年龄
- 阿里巴巴线上面试总结
- java健身房管理系统业务_基于SSM的健身房管理系统
- Centos7防火墙与IPTABLES详解
- 初级测试工程师面试指南
热门文章
- 20P49 Premiere制作信号损坏故障动画节奏运动风格宣传视频开场片头下载
- 多线程练习:铁路售票(线程安全)
- OR-Tools求解仓库选址和钢材取料问题
- oracle中给数据排序号,oracle中对排序的小结
- [亲测]Oracle查询--单表查询,连接查询(一)
- 用户体验之忘记密码之后
- python自动登录教程_python+selenium实现163邮箱自动登陆的方法
- 黑白照片上色软件app有哪些?这几款软件简单易上手
- crontab安装和用法(定时任务)
- java如何手撕加载字节码的代码?编写一个加载class文件的方法