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.

Subscribe to see which companies asked this question

【思路】

1. 举个例子[1,1,1,2,2,2,3,4,5],我的想法就是找到每一节重复数字的长度,然后把后面的数字向前移动,直到遍历到数组最后。

上述例子中,我们从头开始发现有1重复出现了3次,因此我们把1后面的数字向前移动2,变为[1,2,2,2,3,4,5],然后把数组的len变为len-2,重复上面的结果直到遍历到最后。但是这个方法的效率并不高,每发现一个重复的元素都要把后面的数字向前移动,有没有更好的思路呢?

2. 一个更好的方法是:我们维持两个变量,i 用来遍历数组,j 用来指示数组中不重复的那部分的最后一个值的下标。在遍历数组的过程中,如果当前值和前一个值不同,则nums[++j] = nums[i],否则的话继续向前遍历。形象化的过程如下:

  • j = 0; i = 1;

  • nums[i] 等于 nums[i-1];

  • nums[i] 不等于 nums[i-1]; nums[++j] = nums[i];

  • 省略若干步


【java代码1】

 public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length==0) return 0;
int len = nums.length;
int duplen = 0;
for(int i = 0; i < len - 1; i++){
duplen = 0;
for(int j = i + 1; j < len; j++){
if(nums[j] == nums[i]) duplen++;
else break;
}
if(duplen > 0){
for(int k = i + duplen + 1; k < len; k++){
nums[k-duplen] = nums[k];
}
len = len - duplen;
}
}
return len;
}
}

【java代码2】

 public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0)
return 0;
int j = 0;
for(int i=1; i<nums.length; i++) {
if (nums[i-1] != nums[i])
nums[++j] = nums[i];
}
return j;
}
}

LeetCode OJ 26. Remove Duplicates from Sorted Array的更多相关文章

  1. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  2. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  3. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  4. 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array

    一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...

  5. LeetCode OJ 80. Remove Duplicates from Sorted Array II

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

  6. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  7. 【LeetCode】26. Remove Duplicates from Sorted Array

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

  8. 【LeetCode OJ】Remove Duplicates from Sorted Array

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

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

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

随机推荐

  1. 验证码计时 -- UIButton setTitle 闪烁问题解决方案

    首先,有各种版本 方法一: 我运用的一种极其简单的版本:  将UIButton的Type 设成 Custom 就不会有闪烁的问题重现 p.p1 { margin: 0.0px 0.0px 0.0px ...

  2. 菜鸟进阶——grunt

    为保证作者版权在此声明本文部分摘自 http://yujiangshui.com/grunt-basic-tutorial/   另,参考文章 http://www.tuicool.com/artic ...

  3. Java jvm 原理

    1.Java语言运行的过程 Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行. 也相当与 ...

  4. Spring Security(06)——AuthenticationProvider

    目录 1.1     用户信息从数据库获取 1.1.1    使用jdbc-user-service获取 1.1.2    直接使用JdbcDaoImpl 1.2     PasswordEncode ...

  5. keycode 锁键盘按键(只能输入数字)

    $('#lottoStage').keydown(function(){ if(event.keyCode == 46 || event.keyCode==8){ $(this).val(''); r ...

  6. WiMAX协议栈

    1.协议栈模型 协议栈模型将 WiMAX 系统分为数据控制平面和管理平面两个平面 数据控制平面对数据的正确传输进行保证,包括封装.分片.加密.解封装等 基站与用户站之间的特定信令交互完成系统的控制功能 ...

  7. onkeyup事件

    当用户释放键盘按钮时执行Javascript代码. 上代码: <input type="text" id="frame" onkeyup="my ...

  8. HUST 1404 Hamming Distance(字符串)

    Hamming Distance Description Have you ever heard of the Hamming distance. It is the number of positi ...

  9. chapter11_2 Lua链表与队列

    链表 由于table是动态的实体,所以在Lua中实现链表是很方便的.每个节点以一个table来表示,一个“链表”只是节点table中的一个字段. 该字段包含了对其他table的引用.例如,要实现一个基 ...

  10. 大数据阶乘(The factorial of large data)

    题目描述 Description 阶乘是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(long long类型)的一个数,求它的阶乘,请给出正确结果.为提高速 ...