问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3622 访问。

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

给定数组 nums = [1,1,2],

函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。

你不需要考虑数组中超出新长度后面的元素。

给定 nums = [0,0,1,1,1,2,2,3,3,4],

函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。

你不需要考虑数组中超出新长度后面的元素。


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.

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.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3622 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = { 1, 1, 2 }; Console.WriteLine(RemoveDuplicates(nums)); ; Console.ReadKey();
} private static int RemoveDuplicates(int[] nums) {
if(nums.Length == 0) return 0;
int index = 0;
for(int i = 1; i < nums.Length; i++) {
if(nums[i] != nums[index]) {
index++;
nums[index] = nums[i];
}
}
return index + 1;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3622 访问。

2

分析:

显而易见,以上参考算法在最坏的情况下的时间复杂度为:  。

C#LeetCode刷题之#26-删除排序数组中的重复项(Remove Duplicates from Sorted Array)的更多相关文章

  1. ​LeetCode 26:删除排序数组中的重复项 Remove Duplicates from Sorted Array

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. Give ...

  2. LeetCode:删除排序数组中的重复项 (Remove Duplicates from Sorted Array)

    public class RemoveDuplicates { /** * 修改数组,使数组有序不重复.超出长度不考虑. * @param 排序数组 * @return 数组不重复数的个数 */ pu ...

  3. [Swift]LeetCode26. 删除排序数组中的重复项 | Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  4. Leetcode题库——26.删除排序数组中的重复项

    @author: ZZQ @software: PyCharm @file: removeDuplicates.py @time: 2018/9/23 13:51 要求: 给定一个排序数组,你需要在原 ...

  5. 【leetcode算法-简单】26. 删除排序数组中的重复项

    [题目描述] 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完 ...

  6. 前端与算法 leetcode 26. 删除排序数组中的重复项

    目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...

  7. 【Leetcode】【简单】【26. 删除排序数组中的重复项】【JavaScript】

    题目描述 26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 ...

  8. Java实现 LeetCode 26 删除排序数组中的重复项

    26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...

  9. LeetCode初级算法之数组:26 删除排序数组中的重复项

    删除排序数组中的重复项 题目地址:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 给定一个排序数组,你需要在 ...

随机推荐

  1. [jvm] -- 类文件结构篇

    类文件结构 结构图  魔数 头四个字节,作用是确定这个文件是否为一个能被虚拟机接收的 Class 文件. Class 文件版本 第五和第六是次版本号,第七和第八是主版本号. 高版本的 Java 虚拟机 ...

  2. Dart中final和const关键字

    final和const 如果您从未打算更改一个变量,那么使用 final 或 const,不是var,也不是一个类型. 一个 final 变量只能被设置一次,两者区别在于:const 变量是一个编译时 ...

  3. Markdown画图(mermaid)学习

    简介 目前博客园支持mermaid的graph,subgraph,sequenceDiagram,gantt,classDiagram mermaid(美人鱼), 是一个类似markdown,用文本语 ...

  4. MySQL组复制MGR(四)-- 单主模式与多主模式

    (一)概述 组复制可以运行在单主模式下,也可以运行在多主模式下,默认为单主模式.组的不同成员不能部署在不同模式下,要切换模式,需要使用不同配置重新启动组而不是单个server. 相关参数如下: # 该 ...

  5. laravel 迁移文件中修改含有enum字段的表报错解决方法

    解决方法: 在迁移文件中up方法最上方加上下面这一行代码即可: Schema::getConnection()->getDoctrineSchemaManager()->getDataba ...

  6. matplotlib图表介绍

    Matplotlib 是一个python 的绘图库,主要用于生成2D图表. 常用到的是matplotlib中的pyplot,导入方式import matplotlib.pyplot as plt 一. ...

  7. SpringBoot集成Dubbo+Zookeeper

    目录 Spring版本 dubbo_zookeeper负责定义接口 dubbo_provider 服务提供者 dubbo_consumer服务使用者 Spring版本 不知道为啥,新创建的Spring ...

  8. logging日志基础示例

    import logging logger = logging.getLogger() # 获取日志对象 logfile = 'test.log' hdlr = logging.FileHandler ...

  9. isinstance方法判断可迭代和迭代器

    from collections import Iterable print(isinstance([],Iterable)) print(isinstance( {}, Iterable)) pri ...

  10. Python raw_input() 函数

    python raw_input() 用来获取控制台的输入.高佣联盟 www.cgewang.com raw_input() 将所有输入作为字符串看待,返回字符串类型. 注意:input() 和 ra ...