leetcode 80. Remove Duplicates from Sorted Array II

攻防世界GFSJ1184_welcome_CAT_CTF 由于它是 PIE 可执行文件,程序加载地址会随机化,这导致你在 GDB 调试时需要处理地址偏移(如 0x246d 变成 0x55555540246d)。:client 程序通过网络连接(get_nc() 和 get_flag(nc))与 server 通信,server 监听在端口 8888。:client 依赖外部共享库(如 libstdc++.so),因此运行时需要这些库存在系统中。程序就是wasd移动,走到不同标记(* $ @ )下方,输入j回车,会有不同反应。两个文件client和server。 阅读详情

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;
    }
UE5 C++教程(一、基本结构) 虚幻API由三块组成,分别是Runtime,Developer utilitiew,functionality for the Editor,大多数游戏性编程只使用运行时模块,最常见的三个是Corn、CornUObject和Engine。 阅读详情

相关推荐

史上最全的Logisim运算器实验教程(基于华中科技大学计算机组成原理实验)

本博客是实现了Logisim的运算器设计,有具体的运算器设计的思路和运算器设计的电路,适合一步一步的按照思路做。

qq_62325622的博客 4万+

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...

中华小当家的博客 291

SpringBoot3 + Vben5.0实战:从零搭建企业级管理后台(附完整代码)

本文详细介绍了如何从零开始,使用SpringBoot3与Vben5.0技术栈搭建现代化企业级管理后台。通过对比分析,阐述了该组合在开发效率、性能(如虚拟线程)和权限模型(RBAC)方面的优势,并提供了从项目初始化、核心功能开发到部署运维的完整实战指南,帮助开发者快速构建高性能、易维护的后台系统。

weixin_29306317的博客 171

【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的博客 335

AU混响终极指南:从‘干声’到‘空间感’,用总音轨和发送技巧打造专业人声

本文详细介绍了如何在Adobe Audition(AU)中使用混响效果器,通过总音轨和发送技巧将‘干声’转化为具有专业‘空间感’的人声。从混响基础原理到总线架构设置,再到精细控制和自动化调整,全面解析了提升音频质量的实用技巧,适用于音乐制作、播客和内容创作。

weixin_30512089的博客 331

大模型的安全发展与安全治理

2022 年底,OpenAI 推出的 ChatGPT 在文本生成领域实现了重大突破,引领了大模型技术的发展方向,推动了各式各样的基座大模型及行业大模型的发展,并逐步向多模态和具身化方向迅速演进。显然,大模型展现出的强大能力为人工智能通用化揭开了序幕。然而,大模型技术的安全性也面临着生成内容的安全性、伦理道德等重大挑战。只有积极确保大模型的安全性,才能更有效地推动其持续发展,进而更好地应对大模型技术变革所带来的机遇与挑战。

qq_41432686的博客 2582

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 284

嵌入式开发避坑指南:手把手教你用STM32CubeMX配置W9825G6KH SDRAM(含时序参数详解)

本文详细介绍了如何使用STM32CubeMX配置W9825G6KH SDRAM,包括硬件连接检查、CubeMX初始化步骤、时序参数优化及稳定性问题排查。通过实测波形分析和案例分享,帮助开发者避免常见配置错误,提升嵌入式系统性能与稳定性。

weixin_42542669的博客 191

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

如何用 Python 实现自动化测试?详细步骤和代码示例

Python自动化测试实战指南 本文系统介绍了使用Python构建自动化测试体系的完整方案。主要内容包括: 环境搭建:Python 3.8+虚拟环境配置,安装pytest、Selenium等核心测试库 分层测试实现: 单元测试:通过计算器示例演示pytest断言和异常测试 API测试:使用requests库进行RESTful接口验证 UI测试:Selenium WebDriver实现网页交互测试 高级特性: 使用fixture管理测试依赖 参数化实现数据驱动测试 生成HTML和Allure测试报告 GitH

m0_64424835的博客 448

Claude Code(1):手把手教你安装配置 Claude Code

Claude Code 是一款强大的 AI 编程助手工具,能够帮助开发者提升编码效率。本文将详细介绍如何在不同操作系统上安装和配置 Claude Code,让你快速上手这款工具。

blues_C的博客 744

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万+

模型可解释性分析:基于SHAP的深度探索

SHAP模型可解释性分析摘要 本文系统性地介绍了SHAP(SHapley Additive exPlanations)方法在机器学习模型可解释性分析中的应用。SHAP基于博弈论的Shapley值理论,为特征重要性评估提供了统一框架。文章详细阐述了四种核心算法:Kernel SHAP(模型无关方法)、Tree SHAP(针对树模型的高效算法)、Deep SHAP(适用于深度学习)和Linear SHAP(线性模型专用)。通过Python实现展示了从数据准备到SHAP分析的全流程,包括摘要图、依赖图等可视化技术

huanghm88的专栏 606

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的博客 201

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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的博客 293

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【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的博客 643

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

LeetCode80——Remove Duplicates from Sorted Array II

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

上善若水 699

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

皮斯特劳沃 1555

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 1988

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的专栏 586

【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的博客 209

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、付费专栏及课程。

余额充值