leetcode 80. Remove Duplicates from Sorted Array II

Unity+ARFoundation 探坑之路 前两天想要上手Unity来AR方面的项目东东 ,探索过程中出现了各种各样的问题,现在将这个过程中遇到的各种各样问题梳理一下,希望后来者能开开心心上手Unity+AR。 注意:这是一篇踩坑博客,具体搭建还请自行百度。 一、ARFoundation or ARCore or ARKit Unity作为一个平台,不直接提供XR(包括VR、AR和MR)的开发包,这时就需要其他公司提供各种各样的SDK工具包... 阅读详情

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.

给出一个sorted数组,只允许每个元素最多重复两次,问这种情况下数组的长度可以压缩到多少。不能用额外的数组来保存,必须在原数组上改动元素,在返回的长度范围内数字要是正确的(sorted,元素最多重复两次)。

思路:
因为不能分配额外的空间,所以只有把后面的元素向前移,被移动的元素位置和移动到的位置需要两个指针,所以考虑双指针。

指针的开始位置从第3个元素开始,因为前两个元素就算重复也无所谓,不需要改动。

用指针index表示现在指向的元素,last表示将要移动到的位置,即最后一个重复元素的后面
因为last表示重复元素的后面一个,所以
把nums[index]和nums[last - 1], nums[last - 2]比较,

比如[1, 1, 1, 2, 2, 3], 开始时index和last都指向第3个1
nums[index]和nums[last - 1], nums[last - 2]比较发现都相同,这时候说明现在的1是需要去掉的,所以保持last在当前位置不变,方便后面的元素移动到这里把1去掉,index++
index移动到第1个2,和nums[last - 1], nums[last - 2]比较发现不全相同,这时候说明该元素不是和前两个重复的,移动过去不会造成连续重复,所以把当前index指的2移动到last的位置
移动完以后index++, last++

直到index到达右边界

    public int removeDuplicates(int[] nums) {
        if (nums == null) {
            return 0;
        }
        
        if (nums.length < 3) {
            return nums.length;
        }
        
        int index = 2;
        int last = 2;
        
        while (index < nums.length) {
            if (nums[index] != nums[last - 1] || nums[index] != nums[last - 2]) {
                nums[last] = nums[index];
                last ++;
            }
            index ++;
        }
        return last;
    }

版本2,index指需要替换掉的数字位置,i是遍历数组的下标。
i 和index都从下标2开始,因为是排好序的数组,只需要把当前数字nums[i] 和 nums[index - 2]比较,而忽略掉nums[index - 1], 因为如果nums[i] == nums[index - 2], 而且是升序的,就证明这俩数字之间的数字都是和它们相等的,所以需要替换掉Index处
如果nums[i] > nums[index - 2] (注:只有大于,等于两种情况, 因为是升序的),那么要么情况(1) nums[index -2] 和 nums[index -1]相等, 而与nums[i]不等,最多有两个相等,所以不需要替换,情况(2)都不相等,也不需要替换
因此只有 nums[i] == nums[index - 2] 时是需要替换元素的,替换完后index++

    public int removeDuplicates(int[] nums) {
        if(nums == null) {
            return 0;
        }
        
        if(nums.length <= 2) {
            return nums.length;
        }
        
        int index = 2;
        for(int i = 2; i < nums.length; i++) {
            if(nums[i] > nums[index - 2]) {
                nums[index] = nums[i];
                index ++;
            }
        }
        
        return index;
    }
静态代码审计插件 snyk 使用教程 阅读详情

相关推荐

嵌入式开发新思路:用QP框架5步搞定状态机设计(附STM32移植指南)

本文介绍了在嵌入式开发中使用QP框架进行状态机设计的新思路。通过对比传统RTOS与事件驱动模型的差异,详细解析了QP框架的核心组件与优势,并提供了基于STM32的5步实战指南,包括环境搭建、状态机实现与性能优化技巧,帮助开发者从顺序阻塞思维转向高效的事件驱动架构。

milk5的博客 916

leetcode80. Remove Duplicates from Sorted Array 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...

中华小当家的博客 293

STM32 A/B分区OTA升级实战:从原理到避坑指南

本文详细解析STM32 A/B分区OTA升级的核心原理与实战实现,涵盖双分区设计、Bootloader开发、固件传输验证及常见问题解决方案。重点介绍通过A/B分区实现无缝升级与安全回滚的机制,帮助开发者规避典型陷阱,提升嵌入式系统远程更新的可靠性与效率。

qqq44的博客 1126

【LeetCode】解题80:Remove Duplicates from Sorted Array II

LeetCode解题 80:Remove Duplicates from Sorted Array II Problem 80: Remove Duplicates from Sorted Array II [Medium]解题思路Solution (Java)修改过程 Problem 80: Remove Duplicates from Sorted Array II [Medium] Give...

Fayedily的博客 343

Faster Planner——Kinodynamic Astar详解

实际成本和启发式成本 实际成本 Actual Cost   每条轨迹的真实代价函数定义如下: J(T)=∫0T∥u(t)∥2dt+ρT \mathcal{J}(T)=\int_{0}^{T}\|\mathbf{u}(t)\|^{2} d t+\rho T J(T)=∫0T​∥u(t)∥2dt+ρT 离散形式下: 启发成本 Heuristic Cost pμ∗(t)=16αμt3+12βμt2+vμc+pμc=pμc+vμc+T2∗((2∗vμc−2∗vμg)/(2∗T)−(6∗pμc−6∗pμg+6∗T∗v

漫长IT路 4848

携程景点评论数据爬取实战:破解AJAX动态加载与分页参数逆向分析

本文详细介绍了携程景点评论数据爬取的实战经验,重点破解了AJAX动态加载与分页参数逆向分析的技术难题。通过分析`getCommentCollapseList`接口,解析动态参数如`poiId`和`pageIndex`,并实现分页逻辑,帮助开发者高效获取评论数据。文章还提供了完整的Python爬虫代码和反爬策略,适用于数据分析与内容挖掘。

weixin_29098367的博客 683

Leetcode 80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

Leetcode 80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II 标签: Leetcode 题目地址: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/ 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出...

CoderWangSon 286

PIDNet实战:如何在Cityscapes数据集上实现93.2 FPS的实时语义分割(附完整训练代码)

本文提供了在Cityscapes数据集上实现实时语义分割模型PIDNet的完整工程实践指南。PIDNet凭借其独特的比例-积分-微分三分支架构,在CVPR 2023上取得了速度与精度的出色平衡。文章详细介绍了从环境配置、数据预处理、模型训练到性能优化与部署的全流程,并分享了实现93.2 FPS高推理速度的关键技巧与代码。

weixin_29047919的博客 397

LeetCode: 80. Remove Duplicates from Sorted Array II

LeetCode刷题:80. Remove Duplicates from Sorted Array 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 allo...

u011079143的博客 175

组态王与DLT645-2007电能表通讯调试总结

组态王与电力协议DLT 645-2007调试总结 DLT 645-2007是多功能电能表通信协议,该标准由中国电力企业联合会2006年提出,该协议为主-从结构的半双工通信方式。手持单元或其它数据终端为主站,多功能电能表为从站。每个多功能电能表均有各自的地址编码。通信链路的建立与解除均由主站发出的信息帧来控制。帧是传送信息的基本单元,每帧由帧起始符、从站地址域、控制码、数据域长度、数据域、帧信息纵向校验码及帧结束符7个域组成,每部分由若干字节组成,帧格式如图1所示。 1.确定设备地址 地址域由 6 个字节构

逍遥子的博客 1万+

语音算法面试复习系列3——语音识别基础 + CTC 详解

摘要:本文系统讲解了语音识别(ASR)的核心算法CTC。主要内容包括:1) ASR的核心挑战是输入音频帧与输出文字序列的对齐问题;2) CTC通过引入blank标记和动态规划算法,无需对齐标注即可训练;3) 详细解析CTC前向算法实现路径概率求和,以及贪心和Beam Search解码方法;4) 指出CTC的条件独立性假设导致无法建模输出依赖关系;5) 对比CTC与传统方法和Attention/Transducer的优缺点,分析实际应用中的subsampling技术。文章包含数学推导、代码实现和面试常见问题,

m0_60605453的博客 318

Leetcode 80 Remove Duplicates from Sorted Array 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...

herosunly的博客 1万+

把机器学习“搬进”数据库:SAP HANA PAL 快速上手与 ABAP AMDP 实战

摘要:本文探讨了SAP HANA PAL(Predictive Analysis Library)在企业数据分析中的实践应用。PAL通过将机器学习算法下推到数据库内部执行,有效解决了大数据量场景下的数据搬运瓶颈和治理成本问题。文章详细介绍了PAL的核心功能(包括聚类、分类、回归等10类算法)、使用前提(版本、组件和权限要求)以及通过AMDP实现ABAP与PAL集成的技术方案。重点阐述了参数表设计理念和两个典型应用场景:利用PageRank算法分析企业权限网络关系,以及通过PAL_SCALE进行特征预处理。文

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 5754

No.220 - LeetCode[80] Remove Duplicates from Sorted Array II - 数组去重

核心思想:两个游标 /* * @lc app=leetcode id=80 lang=cpp * * [80] Remove Duplicates from Sorted Array II */ // @lc code=start class Solution { public: int removeDuplicates(vector<int>& nums) { int N = nums.size(); if(N<=0) retur

ShellDawn的博客 204

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Leetcode P80 Remove Duplicates from Sorted Array II @python Lang (LL)

Link: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ Content: Method: two pointers (slow and fast pointers) Algorithm: same with P26 单独讨论长度为0,1,2的情况 slow pointer从i = 1开始,fast ...

weixin_44213218的博客 295

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

【leetcode c++】80 Remove Duplicates from Sorted Array II

临近期末,转移战场,考完回家,家里怒报驾校,应接不暇。进正题 Remove Duplicates from Sorted Array II Follow up for "RemoveDuplicates": What if duplicates are allowed at mosttwice? For example, Given sorted array nums = [1,1,

lif的博客 647

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LeetCode80——Remove Duplicates from Sorted Array II

LeetCode80——Remove Duplicates from Sorted Array II 题意: 让非递减的有序数组中重复的数字不超过2个。 思路: 两个索引i和j i是大循环 j是每组相同数字内部为了统计遍历 就是从头到尾遍历并计数,因为是有序的,计数都是连续的比较方便。 一旦计数变量count超过2,则需要进行以下操作: 1.删去该元素 2.j--  

上善若水 701

LeetCode 80. Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II Medium 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 spa...

da_kao_la的博客 146

Leetcode_80_Remove Duplicates from Sorted Array II

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43835055 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array

皮斯特劳沃 1557

leetcode 80 Remove Duplicates from Sorted Array II

题目链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题目原文 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted ...

dff505045的博客 119

LeetCode(80) Remove Duplicates From Sorted Array II

题目如下: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now

BUILD 1990

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间。 C++ 献上自己丑陋无比的代码。相当于自己实现一个带计数器的unique函数 class Solution { public: ...

weixin_30338497的博客 230

LeetCode 80 Remove Duplicates from Sorted Array II

题目 分析 对待这种题我一般就是把需要除去的数赋予一个很大的值,最后经过一个sort,所有不需要的数就到最后面去了。 题解 class Solution { public: int removeDuplicates(

wangxiaobupt的专栏 587

【Java】【LeetCode】80. Remove Duplicates from Sorted Array II

题目: Given a sorted arraynums, remove the duplicatesin-placesuch that duplicates appeared at mosttwiceand return the new length. Do not allocate extra space for another array, you must do this b...

zhelong19830726的博客 211

LeetCode80 Remove Duplicates from Sorted Array II

题目: Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at mosttwice and return the new length. Do not allocate extra space for another array, you ...

zhangjun62的博客 191

LeetCode No.80 Remove Duplicates from Sorted Array II

LeetCode No.80 Remove Duplicates from Sorted Array II

黄健伟的博客 333
上一篇: leetcode 90. Subsets II (2 ways)
下一篇: Java Singleton Design Pattern (单例模式 SE7 Gold)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址