Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >leetcode: explore-array-21 从排序数组中删除重复项

leetcode: explore-array-21 从排序数组中删除重复项

作者头像
用户7685359
发布于 2020-08-24 08:22:58
发布于 2020-08-24 08:22:58
2K00
代码可运行
举报
文章被收录于专栏:FluentStudyFluentStudy
运行总次数:0
代码可运行

leetcode explore 初级算法第一题:从排序数组中删除重复项。

题目分析

这里把题目贴出来:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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]);
}

一大片的英文字母…

我们来提练下题目的意思:

1、输入:是一个列表,同时是一个 sorted array nums,即排好序的列表,并且列表中只包含数字 2、输出:一个整数,这个整数是将列表中元素进行去重后的实际个数 3、in-place,这个单词经常在数组类的题目中出现,即原地修改数组,Do not allocate extra space for another array,两者意思是等价的 3、注意看 Clarification 这段话,它说明了题目的另一个要求,和 in-place 是一致的,即题目虽然输出是一个数字,但会去检查函数传入的那个列表,要求它的前 n 项必须依次是不重复的数字。

按照我们提练的题目意思,我们来看下题目中给的例子,计算步骤是什么样的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
nums = [0,0,1,1,1,2,2,3,3,4]

显然去重后,元素个数为 5

nums 需要依次进行去重,且只能在 nums 上进行修改
而最终 nums 的前 4 位必须是:

[0,1,2,3,4, ...],先后顺序也需要保持。

4后面之所以是省略号,是因为题目并不在乎后面的数字是什么。
It doesn't matter what values are set beyond the returned length.

同样,答案的检验也可以通过题目中给出来的代码来验证,代码是 Java 写的,但理解起来应该还是很容易的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 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]);
}

参考实现

题目看着很长,但其实很简单,实现的方法也很多,比如通过字典,如果要保证顺序也可以使用 OrderedDict,也可以简单的通过遍历来实现。参考代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0

        i = 0
        j = 0
        while i < len(nums):
            f = nums[i]
            while i < len(nums) and nums[i] == f:
                i += 1
            nums[j] = f
            j += 1
        return j

说明

这个题目其实是简化过的,因为它的前提条件就是这个列表是 有序 的,这也提示我们,如果题目稍微换下,变成任意顺序的数组,我们要想到可以通过 排序 来简化题目。

点击阅读原文可查看题目

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FluentStudy 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【leetcode系列】26. 删除排序数组中的重复项
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/
前端迷
2019/07/23
3630
【leetcode系列】26. 删除排序数组中的重复项
LeetCode-26.Remove Duplicates from Sorted Array | 删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
Zoctopus
2021/02/25
4790
算法养成记:删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
三哥
2020/03/12
4170
【Leet Code】26. Remove Duplicates from Sorted Array
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
韩旭051
2019/11/08
3470
LeetCode第27题
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
用户3112896
2019/09/26
3770
算法养成记:移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
三哥
2020/03/26
3490
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.
yesr
2019/03/20
3340
【LeetCode每日一题】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.
公众号-不为谁写的歌
2020/07/28
1820
【leetcode刷题】T5-Remove Duplicates from Sorted Array
今天分享leetcode第5篇文章,也是leetcode第26题—Remove Duplicates from Sorted Array,地址是:https://leetcode.com/problems/remove-duplicates-from-sorted-array/
木又AI帮
2019/07/17
3140
Q26 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 by modifying the input array in-place with O(1) extra memory. Example: G
echobingo
2018/04/25
4890
​LeetCode 26:删除排序数组中的重复项 Remove Duplicates from Sorted Array
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
爱写bug
2019/07/15
4130
LeetCode 0026 - 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.
Reck Zhang
2021/08/11
2200
​LeetCode刷题实战26:删除排序数组中的重复项
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/01/20
2870
Leetcode 题目解析之 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.
ruochen
2022/01/09
1.2K0
leetcode: 26. Remove Duplicates from Sorted Array
leetcode: 80. Remove Duplicates from Sorted Array II
JNingWei
2018/09/28
2730
LeetcCode 27:移除元素 Remove Element(python、java)
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。
爱写bug
2019/07/02
4850
【leetcode刷题】T6-Remove Element(移除元素)
今天分享leetcode第6篇文章,也是leetcode第27题—Remove Element,地址是:https://leetcode.com/problems/remove-element/
木又AI帮
2019/07/17
3390
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 place with constant memory. For example, Given input array nums =
triplebee
2018/01/12
5530
leetcode 26 Remove Duplicates from Sorted Array
Remove Duplicates from Sorted ArrayTotal Accepted: 66627 Total Submissions: 212739 My Submissions
流川疯
2019/01/18
4360
【每日一题】27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
公众号-不为谁写的歌
2020/07/29
3000
推荐阅读
相关推荐
【leetcode系列】26. 删除排序数组中的重复项
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验