leetcode笔记:Remove Duplicates from Sorted Array

一.题目描述

二.解题技巧

从题目中可知,数组中的元素事先已经过排序,因此一个简单而易于实现的方法是从第二个元素开始对数组元素进行遍历,并判断该元素是否和前面的元素相同。题目要求返回不重复的元素的个数。

算法的一个要求是:返回的数组的元素都是不重复,同时要求remove duplicates in place,这意味着不能重新定义一个数组来保存不重复的元素。假设该数组为A,为了满足这一要求,可以设置一个变量num来保存不重复的元素的个数,然后遍历整个数组A,如果该元素A[i]!=A[i-1]的话,将第i个元素复制到A的num位置,也即是A[num]=A[i],然后将num加1.

主要的注意点是,如果数组的元素个数小于2的话,就可以提前结束函数了。

三.示例代码

#include <iostream>
using namespace std;

class Solution {
public:
    int removeDuplicates(int A[], int n)
    {
        if (n < 2) return 0; // 数组元素个数小于2时无需执行算法
        int num = 0;
        for (int i = 1; i < n; i++) {
            if (A[num] != A[i])
                A[++num] = A[i];
        }
        return num + 1;
    }
};

测试代码:

#include "solution.h"
#include <algorithm>

int main()
{
    int num = 50;
    int number[50];

    for (int i = 0; i < num; i++)
        number[i] = rand() % 10 - 10;
    sort(number, number + num); // 先对数组进行排序
    cout << "输入的数组(已排序):" << endl;
    for (int j = 0; j < num; j++)
        cout << number[j] << " ";
    cout << endl << endl;

    Solution remove;
    int index = remove.removeDuplicates(number, num); // 执行算法

    cout << "去除重复元素后的数组:" << endl;
    for (int k = 0; k <index; k++)
        cout << number[k] << " ";
    cout << endl;
    cout << "数组元素的剩余个数:" << index << endl;
    getchar();
    return 0;
}

一个输出结果:

网上一种使用STL的解决思路如下:

// 算法的时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        return removeDuplicates(A, A + n, A) - A;
    }
    template<typename InIt, typename OutIt>
    OutIt removeDuplicates(InIt first, InIt last, OutIt output) {
        while (first != last) {
            *output++ = *first;
            first = upper_bound(first, last, *first);
        }
        return output;
    }
};

四.体会

这道题本身无需排序,只需判断元素与前一个元素是否相同就知道该元素是不是重复的,同时,记录已经遍历过的元素中不重复的元素的个数的变量也同时记录了下一个不重复的元素在数组中的存放位置。算法实现较为简单,当然,可能存在其他更简单的思路。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-12 20:18:48

leetcode笔记:Remove Duplicates from Sorted Array的相关文章

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

【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] 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][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 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】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