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. linux下卸载和安装mysql数据库的方法

    1.1  MySQL下载 下载地址:http://www.mysql.com/downloads/mysql/5.5.html#downloads 版本:5.1.68 平台:linux general ...

  2. MySQL的保留关键字,使用时尽量避免

    今天用phpmyadmin时,注意到一个提示: 列名 'update' 是一个MySQL 保留关键字. 突然意识到还是应该尽量避免这些保留关键字,也百度了一下.找到了这些关键字,列出来下 使用mysq ...

  3. github的使用与问题

    GIT密钥的生成步骤 一 .设置Git的user name和email: $ git config --global user.name "name" $ git config - ...

  4. SQL防漏洞注入攻击小结

    3///   4/// 判断字符串中是否有SQL攻击代码  5///   6/// 传入用户提交数据  7/// true-安全:false-有注入攻击现有:  8public bool Proces ...

  5. Servlet综述

    Servlet 是在服务器上运行的小程序.这个词是在 Java applet 的环境中创造的.虽然后者已很少被使用,但 servlet 却发展的很好.是一般面试都会常考的知识. 由来 servlet ...

  6. 湖南多校对抗赛(2015.05.03)Problem B: War

    并查集.从后往前加边. #include<stdio.h> #include<string.h> #include<math.h> #include<algo ...

  7. Nodejs(待补充)

    Nodejs从入门到精通(待补充) 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stable 是不是很简单?! n后面也可以跟随版本号比如: n ...

  8. 把json数据 [ { } ] 转为数组

    $str = '[{"rwx_price":388.5,"end_station_name":"长沙","swz_price&qu ...

  9. Android 数据过滤器:Filter

    类图: 通常可以将SearchView和ListView结合,实现数据的搜索和过滤. 1.监听SearchView,SearchView.setOnQueryTextListener(OnQueryT ...

  10. ARC 下面可能导致的内存问题

    一.ARC相对MRC来说,减轻了程序员的大部分内存管理工作,使用ARC的时候也需要十分清除内存管理的原理,不然可能带来一些很难调试的问题.下面是ARC下面需要注意的一些问题 1)对象互相引用,形成引用 ...