删除排序数组中的重复项II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 respectively.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,1,2,3,3],
Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
It doesn't matter what values are set beyond the returned length.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
创建一个游标temp,初始化为第一个元素,创建一个变量res,记录当前表的长度,创建一个变量count,记录元素的重复次数。
遍历表中元素,如果当前访问到的元素与游标不相等,则此元素要加入新的数组中,因此res+1,并且游标需要更新;如果访问到的元素与游标相等,变量count+1,如果count的次数不大于2,说明此元素应该加入新的数组中,若count>2,不做处理,继续访问下一位元素。代码如下:
class Solution {
public int removeDuplicates(int[] nums) {
int length = nums.length;
if(length <= 2)
{
return length;
}
int res = 1;
int temp = nums[0];
int count = 1;
for(int i = 1; i < length; i++)
{
if(nums[i] != temp)
{
nums[res++] = nums[i];
temp = nums[i];
count = 1;
}
else
{
count++;
if(count <= 2)
{
nums[res++] = nums[i];
}
}
}
return res;
}
}
删除排序数组中的重复项II的更多相关文章
- LeetCode 80. 删除排序数组中的重复项 II
LeetCode 80. 删除排序数组中的重复项 II
- Java实现 LeetCode 80 删除排序数组中的重复项 II(二)
80. 删除排序数组中的重复项 II 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O ...
- LeetCode(80):删除排序数组中的重复项 II
Medium! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额 ...
- leetcode刷题-80.删除排序数组中的重复项 II
题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...
- [Swift]LeetCode80. 删除排序数组中的重复项 II | 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.删除排序数组中的重复项 II By Python
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 ...
- **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II
1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...
- LeetCode 80. 删除排序数组中的重复项 II(Remove Duplicates from Sorted Array II)
题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- 每天一道面试题LeetCode 80--删除排序数组中的重复项 II(python实现)
LeetCode 80--删除排序数组中的重复项 II 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输 ...
随机推荐
- 写出float x 与“零值”比较的if语句——一道面试题分析
写出float x 与“零值”比较的if语句 请写出 float x 与“零值”比较的 if 语句: const float EPSINON = 0.00001; if ((x >= - E ...
- 【xinsir】分享一个查找文件的脚手架
program.command('find <name>').action(name => { if (name) { inquirer .prompt([ { type: 'inp ...
- 在VMware下进行的Windows2008操作系统虚拟机的安装
一.VMware虚拟机的安装 首先你需要拥有一款软件VMware,这是一款虚拟机安装软件.Vmware比起Vbox收费较贵,占用资源大,但是拥有大量的资源以及拥有克隆技术,适合新手学习使用,较为专业. ...
- Web for pentester_writeup之SQL injections篇
Web for pentester_writeup之SQL injections篇 SQL injections(SQL注入) Example 1 测试参数,添加 and '1'='1, 'and ' ...
- MySQL系统变量auto_increment_increment与auto_increment_offset学习总结
在MySQL中,系统变量auto_increment_increment与auto_increment_offset是与自增列相关的两个参数变量.在官方文档中,将其划分为Replication Mas ...
- 运用wxs制作微信小程序左滑功能和跳转,性能更优越
锲子 微信小程序自定义左滑功能加上跳转,换成以往,左滑功能的逻辑一般是在js中实现,但在拖动方面,性能并不是那么的流畅.如今,官方新扩展了一套脚本语言wxs,在IOS设备上运行,性能会比JS快2~20 ...
- Pycharm创建项目时 自动添加头部信息
1.打开PyCharm,选择File--Settings 2.依次选择Editor---Code Style-- File and Code Templates---Python Script 3.. ...
- linux防火墙相关。
ubuntu 系统默认已安装ufw. 1.安装 sudo apt-get install ufw 2.启用 sudo ufw enable sudo ufw default deny 运行以上两条命令 ...
- Rxjava2源码解析
1:用法: Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer& ...
- Nginx使用反向代理实现负载均衡
Nginx使用反向代理实现负载均衡 yls 2019-9-20 简介 基于docker容器以及docker-compose,所以需要自学docker在linux环境的基本使用 使用两个tomcat作为 ...