问题:

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. Christmas Trees, Promises和Event Emitters

    今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...

  2. ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事

    从编程的角度来讲,ASP.NET Web API针对CORS的实现仅仅涉及到HttpConfiguration的扩展方法EnableCors和EnableCorsAttribute特性.但是整个COR ...

  3. JavaScript学习笔记之数值

    JavaScript内部,所有数字都是以64位浮点数形式储存,即使整数也是如此.(整数也是通过64浮点数的形式来存储的) 所以,1+1.0=2:且1===1.0的 浮点数不是精确的值,所以涉及小数的比 ...

  4. Redis初级介绍

    1 什么是Redis Redis(REmote DIctionary Server,远程数据字典服务器)是开源的内存数据库,常用作缓存或者消息队列. Redis的特点: Redis存在于内存,使用硬盘 ...

  5. Android开发学习之路-PopupWindow和仿QQ左滑删除

    这周作业,要做一个类似QQ的左滑删除效果的ListView,因为不想给每个item都放一个按钮,所以决定用PopupWindow,这里记录一下 先放一下效果图: 先说明一下这里面的问题: ①没有做到像 ...

  6. LeetCode OJ1:Reverse Words in a String

    问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

  7. selenium结合最新版的sikuli使用

    sikuli安装,下载sikulixsetup-1.1.0.jar,地址:https://launchpad.net/sikuli/sikulix/1.1.0 在装有Java环境的机器上直接双击jar ...

  8. MVC5 网站开发实践 2.2、管理员身份验证

    上次完成了管理员的登录,这次要解决对管理员登录后的验证,采用AuthorizeAttribute属性的方式.之前还要解决几个问题,然后才重写验证类,最后稍微改一下界面. 目录 MVC5 网站开发实践  ...

  9. javascript运动系列第六篇——轨迹和投掷

    × 目录 [1]运动轨迹 [2]拖拽轨迹 [3]投掷 前面的话 一般地,不同的运动形式会产生不同的轨迹.但仅凭肉眼去识别运动轨迹,其实并不是很直观.因此,在页面中显示运动轨迹,是一个重要的问题.物体初 ...

  10. hdu 2222 Keywords Search(AC自动机)

    /* 啥也不说了,直接套模板... */ 1 #include<iostream> #include<map> #include<string> #include& ...