第14&15题 Remove Duplicates from Sorted Array I&II

Remove Duplicates from Sorted Array I

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

错误解答:

public class Solution {
    public int removeDuplicates(int[] A) {
        int length = A.length;
        if(length==0 || length==1) return length;

        for(int i=1; i<length;i++){
            if(A[i-1]==A[i]){
                for(int j=i; j<length-1; j++) A[j]=A[j+1];
                //A[length-1]='\0';

                length--;
                i--;
            }
        }
        return length;

    }
}

虽然答案正确,但是O(n)为n的平方,time limit exceeded。

Solution:

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

这种方法只需遍历一次,分别把只出现了一次的数填到A[next]里,时间复杂度为O(n)。

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":

What if duplicates are allowed at most twice?

For example,

Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

Solution1:

public class Solution {
    public int removeDuplicates(int[] A) {
        int length = A.length;
        if(length<3) return length;
        int times =0;
        for(int i=1; i< length;i++){
            if(A[i-1]==A[i]) times++;
            else{ times=0;}
            if(times>=2){
                for(int j=i; j<length-1; j++){
                    A[j] = A[j+1];
                }
                //A[length-1]= '\0';
                times--;
                i--;
                length--;
            }

        }
        return length;

    }
}

这种方法一发现重复两次以上的元素就删掉该元素,将后面元素整体往前移。虽然通过了,但时间复杂度为n的平方,不是很好的方法。

Solution2:

public class Solution {
    public int removeDuplicates(int[] A) {
        int length = A.length;
        if(length<3) return length;
        int times=0, next=1;
        length=1;
        for(int i=1; i< A.length;i++){
            if(A[i-1]==A[i]) times++;
            else{ times=0;}
            if(times<2){
                A[next]=A[i];
                next++;
                length++;
            }
        }
        return length;
    }
}

这种方法只需遍历数组一次,时间复杂度为O(n)。用times记录一个元素重复次数,用next记录移动完的下标,用length记录移动完的长度。如果times<2,就将A[i]的值移动到A[next],否则什么也不做,只移动下标i。

Note:

看其他同学的答案,有些将新生成的数组尾设为‘\0‘,如代码中注释语句所示。其实在java中数组没有结束符,即使将数组尾设为‘\0‘,数组也不会结束,而是将其中元素值设为0。这道题中改动后的数组主要由数组中元素和返回的int值,也就是数组的新长度决定。

时间: 2024-09-29 00:02:38

第14&15题 Remove Duplicates from Sorted Array I&II的相关文章

leetcode_80题——Remove Duplicates from Sorted Array II(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I 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 memor

leetcode_26题——Remove Duplicates from Sorted Array (string)

Remove Duplicates from Sorted Array Total Accepted: 57887 Total Submissions: 183534My Submissions Question Solution Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocat

leetcode第26题--Remove Duplicates from Sorted Array

problem: 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 ar

怎样防超时——Remove Duplicates from Sorted Array I&amp;&amp;II

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 

Remove Duplicates from Sorted Array I&amp;&amp;II

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

[LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超出新长度后面的元素.示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] 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 consta