[Leetcode] 80. Remove Duplicates from Sorted Array II

TouchGFX在STM32上的移植全过程:超详细版指南 深入讲解将TouchGFX集成到STM32开发环境的每一步,涵盖配置、驱动适配与UI调试。无论是初学者还是进阶开发者,都能快速掌握TouchGFX在嵌入式平台的应用技巧。 阅读详情

Description:
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
由于follow up Problem 26,因此还是要求in-place。

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.
同样,有效长度之后的元素是什么不重要。

Language: C++

解法一:
思路:大体思路与26相同,关键地方在于这里允许最多重复一次,也就是有两个相同元素是允许的。那么就要考虑怎么知道这一次重复的元素是第几个重复的呢?比26稍微复杂的地方在于当判断当前元素与它前面一个元素相等时,如何知道它是第几个重复的呢?很自然的想到用flag变量来控制。flag表示当前元素是第几个重复的。
如果当前元素和它前面一个元素不同时,说明是新的元素(sorted array的特性),此时重置flag为0;如果当前元素和它前一个元素相同时,flag加1,判断当前flag是否等于1,如果是,则说明这个元素是允许的,也就是只重复了一次,否则(因为必定不为0),即大于等于2时,说明这个元素是要被删掉的,也就是当前k位置上的元素是要被replace的,k不动,res减1,直到下一次遍历到一个新的元素,再把那个元素赋给当前k位置。
基本思想:同26,两个指针
Time: O(n)O(n)
Space: O(1)O(1)

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size()==1)
            return 1;
        int k = 1;//保证[0...k)范围里的都没有重复元素
        int res = nums.size();
        int flag = 0;//如果为0或1则在允许的条件中,否则就是需要删除的多余元素
        for(int i = 1; i < nums.size(); i++){

            if(nums[i]!=nums[i-1]){
                flag = 0;//对于新的元素,flag重置为0
                if(k != i)
                    nums[k++] = nums[i];
                else
                    k++;
            }
            else{//当前元素与它前面元素重复
                flag++;
                if (flag==1){
                    if(k!=i)//avoid unnecessay copy
                        nums[k++] = nums[i];//重要!!!!
                    else
                        k++;//说明此时是第二个相同的元素,符合条件此时也是符合条件的,k可以加一   
                }
                else
                    res--;
            }
        }
        return res;   
    }
};


算法细节:
特别需要注意的是,当第一次提交结果出错的原因是,没有考虑到当当前元素与它之前元素重复且它是第一次重复的情况下,也要赋值nums[k] = nums[i], 如下:

Input: [1,1,1,1,3,3]
Expected: [1,1,3,3]
Output: [1,1,3,1]

错误原因就在于当i遍历到第二个3时,k在索引位置3(也就是input的第4个1的位置),当前nums[i]还是符合条件的,所以要把nums[i]赋给nums[k];之所以没考虑到这个问题是因为只想到k=i的时候,这个赋值是unnecessary的,所以为了避免unnecessary copy,可以判断一下k和i是否相等,然后再赋值。

由此可见Medium的题其实是在Easy的基础上多加一两个条件而已,找到思路以后,其实并不难。
这是在Leetcode上独立做出来的第一道Medium的题!纪念一下哈哈!
Happy Coding!

10、皮肤 - 神经记录实验全流程解析 本博客详细介绍了皮肤-神经记录实验的全流程,涵盖实验所需的材料准备和具体操作方法。内容包括细胞外记录设备的设置、合成间质液(SIF)的制备与调节、小鼠后爪皮肤和胫神经的手术解剖步骤、清洁解剖后的记录准备、纤维分离与感受野识别方法,以及神经纤维机械响应的测量。该实验为研究神经纤维的活动与功能提供了重要的技术支持,适用于神经科学研究领域。 阅读详情

相关推荐

用Python实现古诗词填字游戏(三)

古诗词做填字游戏的第三部分代码,也是最核心的部分。实现对诗文和关键字的选定,并确定文字的输出位置。

PosPro的专栏 6117

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

SAP-FICO LSMW实战:批量导入财务凭证与固定资产主数据(AS91)的完整指南

本文详细介绍了SAP-FICO中LSMW工具在批量导入财务凭证与固定资产主数据(AS91)的完整流程与实战技巧。从基础概念、环境准备到财务凭证导入全流程,特别针对AS91历史资产导入的日期逻辑和关键字段映射提供了专项指南,并分享了常见错误排查与性能优化建议,帮助用户高效完成SAP数据迁移任务。

weixin_29230649的博客 504

【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

点云图的学习

一。点云地图拼接 1.1 配置运行环境 首先配置安装opencv3,安装完成后如下图: 然后配置点云图工具pcl,直接使用apt即可安装,安装后如下图: 最后使用点云工具观察点云图: 1.2 原理解析 总的来说由多角度拍摄图片拼接为点云3D图,是通过相机的内参,将像素坐标转化为相机坐标,再通过外参将相机坐标转化为世界坐标系,然后将每张图片的像素绘制填充于同一个世界坐标系下面,形成点云3D图。...

ff_xun的博客 1万+

AUTOSAR汽车电子嵌入式编程精讲300篇-基于加密算法的车载CAN总线安全通信

随着科学技术的不断更新和社会文明的不断进步,汽车已经走入寻常百姓家, 成为当代社会必不可少的交通工具。伴随着互联网技术的飞速发展,汽车也不再 是一个简单的交通工具,已经逐渐发展成为一个集出行、通信、娱乐等功能于一 身的综合智能体,进而推动了车联网产业的蓬勃发展。车联网是在传统汽车的框架上安装了传感器、执行器、控制器等分布式和集 中式控制器,使汽车具备环境感知、协同控制、优化执行等功能,并通过网络技 术实现与车外环境的信息共享,给驾驶带来方便的同时也终将成为可以替代人操 作的新一代汽车[1-3]。

getusushu的博客 2590

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

手把手教你学simulink(8.39)--基于Simulink的步进电机(Stepper Motor)仿真实例

通过这个仿真实例,你可以学习如何使用Simulink构建和仿真步进电机(Stepper Motor)系统。你可以根据实际需求调整模型和参数,进行更复杂的仿真和分析。希望这个例子对你有所帮助,祝你学习愉快!

MHD0815的博客 1051

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

vue - 网站接入微信扫码登录功能详细教程,完整详细流程及完整功能示例源代码(附常见问题的解决方案与二维码样式修改方案)

vue.js项目植入微信登录,vue网页项目怎么做微信扫码登录,VUE项目中的微信扫码登录,VUE中最简单的js调用微信扫一扫功能,vue:如何设计网页的微信扫码登录功能,微信扫码功能官方文档,通过vue方式实现二维码扫码功能,vue调用微信扫码,vue H5 调用微信扫描二维码,Vue前端实现微信扫码登录,vue-wxlogin 微信授权登录组件,网站引入的微信登录框如何修改宽高或其他样式,wxlogin 组件插件怎么改变宽高,vue-wxlogin组件怎么自定义样式,微信网页授权,vue调用微信JSDK

✅ 精选推荐文章 6364

matlab RANSAC拟合圆柱【2025最新版】

RANSAC拟合圆柱

点云侠的博客 3135

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

IPGUARD客户端与应用程序冲突

客户端与应用程序冲突 症状 安装客户端之后,客户端的某些程序无法运行或使用过程中出现异常,卸载客户端后恢复正常。 原因分析 可能是客户端的某些功能与应用程序发生冲突导致。 解决方法 当出现这样的情况时,可以按照以下步骤确认。 步骤1:屏蔽主动注入 在客户端配置策略中,增加配置策略hookapi_disinj,值为1;待策略同步到客户端后,重启客户端确认问题是否解决。如果解决,则转到...

IP-Guard专栏 1410

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

老版QQ原生表情gif动态

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【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

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

#include<iostream> #include<vector> using namespace std; class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.empty()){ return 0; } int index...

zwzcdcll的博客 333

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

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

wangxiaobupt的专栏 586

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

【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] 26. Remove Duplicates from Sorted Array
下一篇: [机器学习] k近邻算法 (kNN)
Peggy_Chang
博客等级 码龄11年 21粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值