[leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间。
解法一:
因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断。
Runtime: 20 ms, faster than 19.12% of C++ online submissions for Remove Duplicates from Sorted Array II.
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
if (nums.size() == 0)
return 0;
int i = 1;
int lastNum = nums[0];
int times = 1;
while (i < nums.size())
{
if (nums[i] == nums[i - 1])
{
++times;
if (times > 2)
{
nums.erase(nums.begin() + i);
continue;
}
}
else
{
times = 1;
lastNum = nums[i];
}
i++;
}
return nums.size();
}
};
解法二:
讨论区看到Stefan Pochmann大神的解法,他的解法一如既往的让人眼前一亮,膜拜啦。
Runtime: 8 ms, faster than 100.00% of C++ online submissions for Remove Duplicates from Sorted Array II.
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
int i = ;
for (int n : nums)
if (i < || n > nums[i - ])
nums[i++] = n;
return i;
}
};
[leetcode] 80. Remove Duplicates from Sorted Array II (Medium)的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
		
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] 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 (从有序序列里移除重复项之二)
		
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 给定一个已经排好序的数组 ...
 - Leetcode#80 Remove Duplicates from Sorted Array II
		
原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...
 - [LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值
		
和第一题不同的地方是,容忍两次重复 虽然题目上说只需要长度,但是否检测的时候如果数组不跟着改变也是不行的 没说清楚题意 自己是用双指针做的,看了大神的答案更简单 public int removeDu ...
 
随机推荐
- 国外优秀的UI设计资源库收集
			
国外优秀的UI设计资源库 网站设计或者说UI设计对于Web上的运用是非常的关键,一个站做得好不好,能不能吸引人的眼球,设计占了不低的地位,但话又说回来,Web前端人员又有多少人是设计专业毕业,具有这方 ...
 - Windows下获取逻辑cpu数量和cpu核数量
			
代码可在Windows NT下正常运行 具体API说明请参照如下文档: GetLogicalProcessorInformation 点击打开链接 点击打开链接 点击打开链接 typedef BOOL ...
 - ViewPager页面滑动,滑动到最后一页,再往后滑动则执行一个事件
			
1.ViewPager在处理滑动事件的时候要用到OnPageChangeListener( 代码:this.viewPager.setOnPageChangeListener(new MyListen ...
 - Qemu搭建ARM vexpress开发环境(二)----通过u-boot启动Linux内核
			
Qemu搭建ARM vexpress开发环境(二)----通过u-boot启动Linux内核 标签(空格分隔): Qemu ARM Linux 在上文<Qemu搭建ARM vexpress开发环 ...
 - YARN分析系列之一 -- 总览YARN组件
			
下图简单明了的描述了hadoop yarn 的功能是如何从 hadoop 中细化出来的. 注:图片来自 https://apprize.info/php/hadoop/9.html Hadoop 从 ...
 - Python自学day-15
			
一.防止页面变形 在改变浏览器大小时,可能会导致里面的元素变形(特别是用百分比设置的宽度). 那么,我们如何解决这个问题? 可以在最外层的元素(例如div)中,设置一个固定像素的宽度,例如: < ...
 - 补习系列(21)-SpringBoot初始化之7招式
			
目录 背景 1. @PostConstruct 注解 2. InitializingBean 接口 3. @Bean initMethod方法 4. 构造器注入 5. ApplicationListe ...
 - prometheus-operator监控Kubernetes
			
Operator Operator是由CoreOS公司开发的,用来扩展 Kubernetes API,特定的应用程序控制器,它用来创建.配置和管理复杂的有状态应用,如数据库.缓存和监控系统.Opera ...
 - 阿里云ssl证书NGINX配置https,wss
			
server { listen 443; server_name server.sentiger.com; ssl on; root /home/wwwroot/Service/beta/public ...
 - Java学习笔记——设计模式之十.观察者模式
			
观察者模式(Observer),定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己. Subject类: ...