算法题丨Remove Duplicates from Sorted Array II
描述
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
示例
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.
算法分析
难度:中
分析:条件参考Remove Duplicates算法,只不过之前元素只允许重复1次,现在改为最多可以重复2次。
思路:既然输入的数组排好序的,那我们定义一个有效元素的数组长度变量i,然后开始遍历元素:
1. 检索当前元素之前的2个元素,如果跟当前元素相等,说明不满足最多重复2次的条件了,这时候有效元素的数组长度i就不要再自增了;
2. 否则的话,之前的2个元素,如跟当前元素不相等(小于),说明这个当前元素为有效值,所以将数组末尾元素赋值当前元素,有效元素的数组长度i自增1;
依照上述逻辑循环判断,一直到数组最后一个元素,循环结束后,根据有效元素的数组长度i,获得[0,i)范围内的数组元素,即为题目要求的结果。
代码示例(C#)
public int RemoveDuplicates2(int[] nums)
{
int i = 0;
foreach (var num in nums)
{
//判断是否有2个以上重复,有的话有效索引+1,并将当前元素赋值到有效数组
if (i < 2 || num > nums[i - 2])
nums[i++] = num;
}
return i;
}
复杂度
- 时间复杂度:O (n).
- 空间复杂度:O (1).
附录
算法题丨Remove Duplicates from Sorted Array II的更多相关文章
- 算法题丨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 Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- 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 (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
随机推荐
- The struts dispatcher cannot be found
1.错误描述 严重:Servlet.service() for servlet jsp threw exception The struts dispatcher cannot be found.Th ...
- freemarker.template.TemplateException:Error executing macro:mainSelect
1.错误描述 freemarker.template.TemplateException:Error executing macro:mainSelect require parameter:id i ...
- 如何给filter添加自定义接口
.在Cfilter类的定义中实现Interface接口的函数的定义: //-----------------------Interface methods----------------------- ...
- tcp_wrapper
介绍 对基于tcp协议开发并提供服务的应用程序,所提供的一层访问控制工具 基于库调用实现其功能 * 库名:libwrap 判断服务是否能够由tcp_wrapper进行访问控制 1. 动态编译 ldd命 ...
- IIS易混概念小结
IIS连接数 常识: 虚拟主机会限制IIS连接数,关于其含义,差不多每个主机供应商都有一套自己的说法,微软也没有给出很明确的解释: 含义: IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫 ...
- 配置文件properties读取使用的好方法
首先在spring配置文件applicationContext.xml中配置. <bean id="placeholderConfig" class="com.be ...
- hdu5937 Equation
题解其实网上有 突然有点感想 为什么可以用搜索或状压,因为方案数很有限,它要求每种方案不同就意味着搜索的次数也一定,所以现在就应该坚定往这方面想,找部分方案的贪心.这和上一题一样,都是先暴力,后面处理 ...
- 通过分析HSL/HSB获取图片主色调
这两天稍微研究了一下颜色的HSL/HSB值,主要因为写程序想要实现通过一张图片拿到图片中的最突出的颜色值(类似Groove Music中播放栏背景就是从专辑封面中取出主色调,还有Windows 10任 ...
- SQL Server将自己的查询结果作为待查询数据子列之一
嵌套子查询是SQL语句中比较常用的一种查询方法,开发过程中遇到查询需要将自己的某列作为待查询的数据,在参考别人的SQL语句的写法终于实现了自己需要的功能. 查询语句如下: SELECT DISTINC ...
- git上传项目到github简易步骤
第一步: 在github创建仓库,创建完成进入该仓库,仓库地址如:https://github.com/winerss/wcMall格式 第二步:使用git,cd到本地项目目录下,执行git明令: g ...