描述

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.

示例

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

算法分析

难度:低

分析:要求给定的排序后的数组,将其中重复的元素去除(是的每个元素只出现一次),并返回最终数组的长度。算法不要分配额外的数组空间。

思路:既然输入的数组已然排好序,我们可以定义i,j两个数组下标值,开始i初始为0,用来记录数组中有效元素下标,j从1开始,用来记录遍历数组的下标:

如果数组[j]==数组[i],表示有重复,重复元素个数i不用累计,遍历数组下一条;

如果数组[j]!=数组[i],表示没有重复,有效元素下标i自增+1,把数组[i]值用数组[j]替代,比较下一个元素;

依次遍历,直到数组[j]至最后一个元素,最终[0,i]范围内的数组元素,即为题目要求的结果。

代码示例(C#)

public int RemoveDuplicates(int[] nums)
{
if (nums.Length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.Length; j++)
{
//不重复的话,有效索引+1,将当前元素值记录在索引对应的数组上
if (nums[j] != nums[i])
{
i++;
nums[i] = nums[j];
}
}
return i + 1;
}

复杂度

  • 时间复杂度O (n).
  • 空间复杂度O (1).

附录

算法题丨Remove Duplicates from Sorted Array的更多相关文章

  1. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  2. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  3. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  4. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  6. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  9. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. 记录k8s:k8s1.8.4无坑离线安装

    安装部署: 1. 使用vagrant 准备3太虚拟机,自己使用Vbox 准备3太也可以. 2. 按照 https://github.com/gjmzj/kubeasz 安装. 3. 使用letsenc ...

  2. Redis 设计与实现 (六)--发布订阅

    发布订阅 相关命令:publish(发布)  subscribe(订阅)  psubscribe(支持通配符匹配) 发布者--频道--订阅者(发布和订阅都是客户端频道是服务端) 1.publish-  ...

  3. mybatis generator自动生成代码时 只生成了insert 而没有其他的

    mybatis框架提供了非常好用的逆向工程插件,但是在使用过程中会有很多问题. 我在使用中就遇到了只生成insert和insertSeletive方法,而不生成其他根据primary key查询更新删 ...

  4. 武汉软件开发:一看就会的wpf入门教程

    据了解,目前武汉软件开发市场关于PC端桌面开发的技术主要有两块:winform和wpf.wpf是微软既winform之后推出的一套新的桌面开发技术.采用数据驱动的方式可以轻松编写出非常炫的界面. 1. ...

  5. Xamarin.Android 水平对齐与垂直对齐

    水平对齐: 1.LinearLayout添加属性:android:orientation="vertical": 2.元件添加属性:android:layout_gravity=& ...

  6. oracle收集ash和awr性能报告方法

    1.收集ash报告 [oracle@1 ~]$ sqlplus / as sysdbaSQL*Plus: Release 12.1.0.2.0 Production on Fri Sep 21 18: ...

  7. source map 的原理探究

    线上产品代码一般是编译过的,前端的编译处理过程包括不限于 转译器/Transpilers (Babel, Traceur) 编译器/Compilers (Closure Compiler, TypeS ...

  8. Java基础2:基本数据类型与常量池

    本文会结合虚拟机对引用和对象的不同处理来介绍三大特性的原理. 三大特性:继承 封装 多态 继承 Java中的继承只能单继承,但是可以通过内部类继承其他类来实现多继承. public class Son ...

  9. ldap配置系列三:grafana集成ldap

    ldap配置系列三:grafana集成ldap grafana的简介 grafana是一个类似kibana的东西,是对来自各种数据源的数据进行实时展示的平台,拥有这牛逼的外观.给一个官方的demo体验 ...

  10. adb server is out of date. killing... ADB server didn't ACK解决方法

    在使用ADT Bundle进Android开发时,有时经常会碰到如下错误提示: adb server is out of date. killing... ADB server didn't ACK ...