【LeetCode】解题26:Remove Duplicates from Sorted Array

Windows系统中 Xmanager 6 企业版下载安装激活教程 Xmanager 6 企业版安装激活教程 1.Xmanager 6 下载网址:https://yunpan.360.cn/surl_yrJrWq3GmjN 2. 双击 Xmanager-6.0.0169r 安装 3. 点击运行按钮 4. 点击下一步 5. 选择我接受许可证协议中的条款(A), 点击下一步。 6. 输入用户名:byg,公司名称:microsoft,序... 阅读详情

Problem 26: Remove Duplicates from Sorted Array [Easy]

Given a sorted array nums, remove the duplicates in-place such that each element appear only once 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,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn’t matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn’t matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

来源:LeetCode

解题思路

  • 使用一个指针index指向需要修改的位置,nums[0]~nums[index-1]代表已经检测过的未重复的数字。
  • 遍历数组,跳过重复的数字,将未重复的数字填入nums[index],指针index向后移一位。
  • 数组个数小于2时直接返回。

时间复杂度O(n)。

要点:双指针

Solution (Java)

class Solution {
    public int removeDuplicates(int[] nums) {
        int N = nums.length;
        if (N < 2) return N;
        int index = 1;
        int last = nums[0];
        for(int i = 1; i < N; i++){
            while(i < N && nums[i] == last) i++;
            if(i == N) break;
            last = nums[i];
            nums[index] = nums[i];
            index++;
        }
        return index;
    }
}

修改过程

  • 跳过重复数字时忘记加上i < N的判断,导致数组越界。
【24届秋招总结】24届数字IC秋招给学弟学妹们的99条建议(标题党) 针对2023年找实习、秋招、谈薪资等方面的个人经验进行总结,希望对后续同学们找工作有所帮助 阅读详情

相关推荐

信创迁移实战:在银河麒麟V10-SP1上为Milvus离线部署Docker(附SP1专属避坑指南)

本文详细介绍了在银河麒麟V10-SP1 x86_64环境下离线部署Docker的全流程,特别针对该版本特有的自定义目录安装失败问题提供深度分析和解决方案。通过实战验证的步骤和避坑指南,帮助用户在信创项目中顺利完成Milvus等应用的容器化部署。

weixin_30687051的博客 532

LeetCode 26 Remove Duplicates from Sorted Array 解题思路和python代码

第一个指针 k 记录现在有多少个unique element,并且记录这个unique element将放在nums的哪一个位置。第二个指针 i 记录现在遍历的nums的位置,检查现在这个element与它前一个element是否相等。nums做的是in-place的修改,前k个数包含的都是unique element。使用两个指针来记录。

weixin_57266891的博客 875

THB6128步进电机驱动芯片介绍

THB6128芯片也称为步进电机驱动芯片,采用BiCDMOS工艺,IC管脚间距为1mm的小型MFP30KR封装而成。

weichen_78的博客 7182

leetcode解题之 26. Remove Duplicates from Sorted Array Java版

Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for anoth...

he_zhen_的博客 321

《统计学习方法》——李航 学习大纲

最近在学习李航写的统计学习方法概论, 每一章都用xmind理清了思路,括号里是书里的公式,第一次写博文,敬请指教~~~~ 第一章 统计学习方法论 第二章 感知机 每个方法其实只需要着重掌握三要素和输入输出就可以了,主要看模型、策略和算法。 感知机主要是二类分类的线性分类模型。 看到后面会注意感知机和支持向量机的区别。 第三章 k近

wjy619251334的博客 1万+

中国1990-2021连续30年土地利用CLCD数据分享

CLCD(China Land Cover Dataset)数据集是由武汉大学黄昕老师基于Google Earth Engine上的335,709景Landsat数据制作的中国年度土地覆盖数据集。为了提高CLCD的时空一致性,还提出了一种包含时空滤波和逻辑推理的后处理方法。该数据集最大的优势在于每年30米的土地利用分类结果,且连续覆盖了30年时间。相比其他产品如GLC_FCS30、Global30、AGLC2000_2015、FROM-GLC10、ESA10、ESRI10等,CLCD在时间分辨率上更高。

qq_48539301的博客 1万+

[leetcode] 26. Remove Duplicates from Sorted Array 解题报告

题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

小榕流光的专栏 434

【Elasticsearch专栏 16】深入探索:Elasticsearch的Master选举机制及其影响因素分析

Elasticsearch的Master选举机制是其分布式架构的基石,确保集群高可用性和数据一致性。通过多数派协议,节点间交换信息并投票选举Master,以维护集群状态和处理请求。该机制自动处理故障转移,保障服务连续性。

weixin_40736233的博客 1774

【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python)

题目分析: 这一题是比较简单,就是返回有序列表中不重复的元素个数,并且将列表的前面项修改为这些不重复元素。 代码说明(1): 1、dict, length = {}, 0,dict用于判断该元素是否出现过,length是返回的长度,也是不重复元素的下标。 2、如果元素不在dict里就表明这是一个未出现过的元素,把它加入dict,将该元素修改为列表的第length个元素,最后使返回的长度,也是不重复...

L141210113的专栏 344

【Unity实战】实现最完美最简单的UI适配方案,背景压缩或者拉伸变形解决方案,手机刘海屏适配,适配不同分辨率,不同尺寸,不同设备。

这期来讲一个简单的UI设计方案,很多同学可能搞不懂锚点、轴心这些概念,导致做好的UI在别人的设备上,乱跑或者是重叠,或者是参加游戏老发时间比较紧来不及做适配,都可以来试试这个方法,通过填充边框来保持游戏画面的原始比例,所以它的缺点也很明显,在一些移动设备上会浪费比较大的显示面积。 unity不同设备UI变形? UI在不同设备上如何适配?

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 3648

Android软键盘系统深度解析:架构、原理与实现

Android软键盘系统深度解析:架构与实现原理 本文系统剖析了Android软键盘的核心工作机制。作为系统级输入服务,软键盘具有双重属性:既是可安装的独立应用,又深度集成于系统框架中。文章详细解读了软键盘的系统架构,包括其与InputMethodManagerService(IMMS)和WindowManagerService(WMS)的交互机制,以及通过InputConnection实现的跨进程通信流程。同时分析了输入法窗口的特殊管理策略、布局调整模式,并深入探讨了输入预测引擎的实现原理,包括词典加载、

保持专注,保持热爱 114

Leetcode #26. Remove Duplicates from Sorted Array 移除重复数字 解题报告

1 解题思想这道题因为是已经排好序的,所以比较简单,相当于设置两个指针,一个用来扫描,一个用来记录当前没有重复的部分的截断位置,当遇到一个全新的值的时候,就将其移动到截断位置那里就好,然后截断位置+1嗯,截断位置永远小于等于扫描的指针那个位置,且覆盖的时候必然不会影响到之前不重复的值2 原题26. Remove Duplicates from Sorted Array Given a sorted

MebiuW的专栏 960

Camunda 7.x 系列【22】全局监听器

Camunda支持义一个全局ExecutionListener和一个全局TaskListener,会捕获流程中发生的所有事件的通知,这样可以定义一个全局的审批人检查监听器,作用于所有用户任务。

记录知识、锤炼自我 2942

[LeetCode]26. Remove Duplicates from Sorted Array 解题报告(C++)

[LeetCode]26. Remove Duplicates from Sorted Array 解题报告(C++) 题目描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not ...

【黑键】 299

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[Leetcode] 26. Remove Duplicates from Sorted Array 解题报告

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in p

魔豆(Magicbean)的博客 465

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

【LeetCode】Remove Duplicates from Sorted Array 解题报告标签(空格分隔): LeetCode[LeetCode]https://leetcode.com/problems/remove-duplicates-from-sorted-array/Total Accepted: 129010 Total Submissions: 384622 Difficul

负雪明烛 1969

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

LeetCode - 解题笔记 - 26 - Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array Solution 1 因为要求原为剔除,所以替换方案只能是交换位置(非链表)。要求输出的结果为不重复子序列的长度,因此将所有不同的元素换到最前面即可。给定的数组已经是有序的,因此同样可以用双指针进行维护。第一个指针为以去重子序列的末尾,第二个指针为欲考察的元素,当二者不同时,在子序列的后一个位置完成替换。需要留意空数组的情形。 时间复杂度:O(N)O(N)O(N),NNN为序列长度 空间复杂度:O(1)O(1)O(1),要求原位处理,

支锦铭的博客 197

【LeetCode】26. Remove Duplicates from Sorted Array 解题小结

题目: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length. Do not allocate extra space for another array, you must do this in place w...

weixin_30279315的博客 120

【LeetCode】26.Remove Duplicates from Sorted Array(Easy)解题报告

【LeetCode】26.Remove Duplicates from Sorted Array(Easy)解题报告 题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 题目描述:   Given a sorted array, remove the duplicates

郝春雨的博客 164

Leetcode1-100: 26. Remove Duplicates from Sorted Array

Leetcode1-100: 26. Remove Duplicates from Sorted Array问题描述解题思路代码实现 问题描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do

Y123iwantit的博客 232

Leetcode 刷题日记(Day1)— Array

之前有陆续刷过部分的 leetcode 题目,但为了春招做准备,决定重新按序刷一遍,也做好总结的工作,方便复习。 参考的刷题顺序是 Leetcode 分类顺序表第二版 ,有一定的参考价值。 LeetCode c++ 提速小技巧:关闭流同步,效果极好。 static const auto speedup = []() { std::ios::sync_with_stdio(false...

Schnee-Cy的博客 334

【LeetCode】 26. Remove Duplicates from Sorted Array 删除排序数组中的重复项(Easy)(JAVA)

【LeetCode】 26. Remove Duplicates from Sorted Array 删除排序数组中的重复项(Easy)(JAVA) 题目地址: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 题目描述: Given a sorted array nums, remove the duplicat...

qq_16927853的博客 175

Leetcode解题之路(golang版):26. 删除排序数组中的重复项(Remove Duplicates from Sorted Array)

26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后...

myz123321的博客 501

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another

Android进阶 849

leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for anot...

weixin_33704234的博客 89

LeetCode小白菜笔记[8]:Remove Duplicates from Sorted Array

LeetCode小白菜笔记[8]:Remove Duplicates from Sorted Array

VisualCortex 438
上一篇: 【LeetCode】解题18:4Sum
下一篇: 【LeetCode】解题27:Remove Element
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值