【LeetCode】080. Remove Duplicates from Sorted Array II
题目:
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.
题解:
Solution 1
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int k = ;
int n = nums.size();
if(n < )
return n;
int index = , cnt = ;
for(int i = ; i < n; ++i){
if(nums[i] != nums[index]){
cnt = ;
nums[++index] = nums[i];
} else if(++cnt <= k){
nums[++index] = nums[i];
}
}
return index + ;
}
};
Solution 2
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int k = ;
int n = nums.size();
if(n < k)
return n;
int index = k;
for(int i = k; i < n; ++i){
if(nums[i] != nums[index - k])
nums[index++] = nums[i];
}
return index;
}
};
此解与Remove Duplicates from Sorted Array相似
【LeetCode】080. Remove Duplicates from Sorted Array II的更多相关文章
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】83 - Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode】026. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- java开发知识体系
- 4 Values whose Sum is 0(二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 21370 Accep ...
- [原创]实现多层DIV叠加的js事件穿透
Flash里面有个很好的特性是,一个容器里,不存在实际对象的部分,不会阻拦鼠标事件穿透到下一层. 前端就不一样了,两个div层叠以后,上层div会接收到所有事件(即使这个div里面内容是空的,没有任何 ...
- 九度OJ 1200:最大的两个数 (最值)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2904 解决:761 题目描述: 输入一个四行五列的矩阵,找出每列最大的两个数. 输入: 输入第一行包括一个整数n(1<=n<= ...
- Linux软件包分类
源代码包 优点: 1.给你的就是源代码 2.可以修改源代码 3.可以自由选择所需的功能 4.软件是在自己电脑上编译安装,所以更加稳定高效 5.卸载方便(直接删了你安装软件的那个目录就好了) 缺点: 1 ...
- 关于align-items和align-content的区别和使用场景
最近在研究flex布局,容器中有两个属性,是用来定义crossAxis测轴排列方式的.一开始接触align-items还可以理解感觉不难,后来看到align-content就感觉有点混淆了,特开一篇博 ...
- Android中子线程真的不能更新UI吗?
Android的UI访问是没有加锁的,这样在多个线程访问UI是不安全的.所以Android中规定只能在UI线程中访问UI. 但是有没有极端的情况?使得我们在子线程中访问UI也可以使程序跑起来呢?接下来 ...
- 小程序连接百度ai
function getTextFromImage(res) { var access_token = '24.c649256d2e*****0.282335-11449805'; var url = ...
- shell一些方法
字符串截取转自原文地址:http://www.jb51.net/article/56563.htm 一:字符串截取 有var变量: var=http://www.aaa.com/123.htm 1. ...
- css 行内元素 块元素 替换元素 非替换元素 以及这些元素的width height margin padding 特性
一.各种元素的width height margin padding 特性(具体css元素的分来参看二) 1.块级元素 width. height. margin的四个方向. padding的四个方向 ...