当前位置:网站首页>Leetcode80. 删除有序数组中的重复项 II
Leetcode80. 删除有序数组中的重复项 II
2022-08-09 23:32:00 【Java全栈研发大联盟】
题目传送地址:https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/
运行效率
代码如下:
class Solution {
public static int removeDuplicates(int[] nums) {
int left = 0;
int right = 1;
int count=0;
//注意:nums的长度是动态变化的,所以下面的while条件里要写成小于 nums.length-count
while (right < nums.length-count && left < nums.length-count) {
if (nums[left] == nums[right]) {
if (right - left == 2) {
moveForward(nums, right);
count++;
} else {
right++;
}
continue;
}
if (nums[left] != nums[right]) {
left = right;
right++;
}
}
return nums.length-count;
}
public static void moveForward(int[] nums, int index) {
for (int i = index; i < nums.length; i++) {
nums[i - 1] = nums[i];
}
}
}
边栏推荐
- 字节技术面都过了,薪资都谈好了20K*13结果还是被刷了,问HR原因是。。。
- 下班后用微信处理工作时发病身亡,法院判决:工伤!
- ES6 从入门到精通 # 13:数组的扩展方法二
- selenium和驱动安装
- Pinduoduo store operation must know to leave a little knowledge of operation
- 使用C语言实现静态链表
- JSON对象和字符串相互转化
- 断开和服务器共享连接的方法「建议收藏」
- YOLOV5 study notes (7) - training your own data set
- Creo5.0 introductory tutorial free material
猜你喜欢
【集训DAY3】石油储备计划【树形DP】
781. 森林中的兔子
ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
ES6 从入门到精通 # 15:生成器 Generator 的用法
YOLOV5 study notes (7) - training your own data set
go语言的并发原理(goroutine)
In-depth understanding of multithreading (Part 1)
vmware Exsi 网卡配置
【Infiltration tool】Browser data export tool
Jpa 查询view or 无主键的table
随机推荐
重估HR SaaS:一体化后的新三年
Today's sleep quality record 61 points
微信小程序获取微信用户步数
Leecode-205. 同构字符串
大龄测试员刚迈过了 35 岁这个“坎儿”,和大家说点儿心里话
巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...
Alibaba Cloud SMS Service Activation
Fury:一个基于JIT动态编译的高性能多语言原生序列化框架
CAD 连接两个相交线
arm-4-裸板开发
如何抑制告警风暴?
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
ES6 Beginner to Mastery #15: Generator Usage
错误提示:Syntax error on token “function”, delete this token
【Infiltration tool】Browser data export tool
知行合一的时候
The older tester has just passed the "hurdle" of being 35 years old, and I want to tell you something from my heart
工程 (七) ——PolarSeg点云语义分割
NotWritableError: The current user does not have write permissions when conda creates a new environment
下班后用微信处理工作时发病身亡,法院判决:工伤!