leetCode-数组:Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素

考察数组的基本操作:

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums==null || nums.length==0)
            return 0;
        int index = 1;
        for(int i =1; i<nums.length; i++){
            if(nums[i]!=nums[i-1]){
                nums[index] = nums[i];
                index++;
            }
        }
        return index;
    }
    public static void main(String[] args) {
      int[] arr = { 1, 2, 2, 3, 3 };
      arr = removeDuplicates(arr);
      System.out.println(arr.length);
}
}

Remove Duplicates from Sorted Array II (Java)

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].

最多允许两个重复,输出结果数组。

解法1:当counter是2时,就直接跳过即可,否则说明元素出现次数没有超,继续放入结果数组,若遇到新元素则重置counter。总体算法只需要扫描一次数组,所以时间上是O(n),空间上只需要维护一个index和counter,所以是O(1)。

public int removeDuplicates(int[] A) {
    if(A==null || A.length==0)
        return 0;
    int idx = 0;
    int count = 0;
    for(int i=1;i<A.length;i++)
    {
        if(A[i]==A[i-1])
        {
            count++;
            if(count>=3)
                continue;
        }
        else
        {
            count = 1;
        }
        A[idx++]=A[i];
    }
    return idx;
}

解法二:

public class Solution {
    public int removeDuplicates(int[] A) {
        if (A.length <= 2)
            return A.length;

        int prev = 1; // point to previous
        int curr = 2; // point to current

        while (curr < A.length) {
            if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
                curr++;
            } else {
                prev++;
                A[prev] = A[curr];
                curr++;
            }
        }

        return prev + 1;
    }
}
时间: 2024-11-08 12:43:52

leetCode-数组:Remove Duplicates from Sorted Array的相关文章

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

leetCode 26. Remove Duplicates from Sorted Array 数组

26. Remove Duplicates from Sorted Array 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 mem

[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, 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

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

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

[LeetCode][JavaScript]Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of n

【leetcode】Remove Duplicates from sorted array

Remove Duplicates from sorted array 问题描述 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 me

LeetCode OJ Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't