问题:

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 = [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.

官方难度:

Easy

翻译:

给定一个已经排序的数组,删除重复元素,返回新数组的长度。程序不关心这个长度之后存储的是什么东西。

例子:

给定数组:[1,1,2]

返回长度:2,且数组以[1,2]开头。

  1. 有2个很关键的信息:数组有序,不关心长度之后的内容。
  2. 不关心长度之后的内容,题目暗示了,在原数组上处理数据的内容。
  3. 由于数组有序,维护一个快指针(从第2项开始),一个慢指针(从第1项开始)。快指针遍历数组,判断两个指针指向的数字是否相同。若相同,不做处理;若不同,慢指针向前移一位,且快指针的值覆盖慢指针的值。
  4. 结束循环,返回长度为慢指针+1。
  5. 入参检查,同时,长度小于2的直接返回。

解题代码:

     public static int removeDuplicates(int[] array) {
if (array == null) {
throw new IllegalArgumentException("Input error");
}
if (array.length < 2) {
return array.length;
}
// 慢指针
int i = 0;
for (int j = 1; j < array.length; j++) {
// 快指针不断前进,遇到与慢指针数字相同的情况,跳过
if (array[j] != array[i]) {
i++;
array[i] = array[j];
}
}
// 返回新数组的长度,不关心之后的内容
return i + 1;
}

removeDuplicates

相关链接:

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q026.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.026:Remove Duplicates from Sorted Array的更多相关文章

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

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

  2. leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

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

  3. leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

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

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

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

  5. LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

    题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear onl ...

  6. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

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

  7. LeetCode OJ:Remove Duplicates from Sorted Array(排好序的vector去重)

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

  8. leetcode解题报告(1):Remove Duplicates from Sorted Array

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

  9. lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II

    题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...

随机推荐

  1. Git Shell 基本命令(官网脱水版)

    用户信息 当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址. 这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改: $ git conf ...

  2. 修改注册表 去除Windows快捷方式图标小箭头

    一些朋友不喜欢Windows系统中快捷方式图标上面的小箭头,下面介绍如何修改注册表去除快捷方式图标上的小箭头. 1.开始->运行->输入regedit,启动注册表编辑器,然后; 2.依次展 ...

  3. iOS开发--应用崩溃日志揭秘(二)

    场景 4: 吃棒棒糖时闪退! 用户邮件说, “当rage master吃棒棒糖时应用就闪退…” 另一用户说, “我让rage master 吃棒棒糖,没几次应用就闪退了!”崩溃日志如下: Incide ...

  4. AngularJS中的表单验证

    AngularJS中的表单验证 AngularJS自带了很多验证,什么必填,最大长度,最小长度...,这里记录几个有用的正则式验证 1.使用angularjs的表单验证 正则式验证 只需要配置一个正则 ...

  5. 使用 fixed role 授予权限

    今天下午,Leader 发mail给我,要求授予某个User对数据库只读的权限. Step1,在SQL Server中为该用户创建一个Login和User,在创建User时,建立Login 和 Use ...

  6. Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办?

    Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办? 编码: Uri.EscapeDataString(name) 解码: ...

  7. No row with the given identifier exists:

    最近在弄一个后台项目,有用到hibernate操作数据库.写hql语句表一对一关联查询的时候报这个错误.受到了csdn上一篇博客的启发,解决了我的问题.他的博客地址:http://blog.csdn. ...

  8. Deferred在jQuery和Angular中的使用与简单实现

    Deferred在jQuery和Angular中的使用与简单实现 Deferred是在jQuery1.5版本中加入的,并且jQuery使用它完全重写了AJax,以前也只是偶尔使用.但是上次在使用Ang ...

  9. 开启SharePoint Server 2013 中的“微博”功能——新闻源

    熟悉SharePoint的朋友在2013之前的版本可以使用社区协作下的记事板.应用程序下的通知,来进行消息的发布,而且更有这两者的完美结合体讨论板,可供使用着根据站点属性进行添加而对现在的快消息时代, ...

  10. JavaScript中Promises/A+规范的实现

    Promises是一种异步编程模型,通过一组API来规范化异步操作,这样也能够让异步操作的流程控制更加容易. 下面的代码是假设执行一个异步队列,每一项都会使用上一项返回的数据: function ne ...