数组(1):Remove Duplicates from Sorted Array

describe:

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

Code 1:

// LeetCode, Remove Duplicates from Sorted Array
// 时间复杂度 O(n) , 空间复杂度 O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
}
};

Code 2:

// LeetCode, Remove Duplicates from Sorted Array
// 使用STL,时间复杂度 O(n),空间复杂度 O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return distance(nums.begin(), unique(nums.begin(), nums.end()));
}
};

Function usage:

 //distance主要是用来求两个迭代器之间的元素个数。
 std::distance
 template<class InputIterator>
  typename iterator_traits<InputIterator>::difference_type
    distance (InputIterator first, InputIterator last);
 //advance()主要是用来使迭代器步进(或步退)n个元素;
 void advance(pos,n);

unique() function:
unique is one of the very practical functions in STL, it needs #include iostream, let's briefly introduce its function. The function of unique is to "remove" the duplicate elements of adjacent elements in the container. Here, a quotation mark should be added to remove them. Why, because it is essentially a pseudo-removal, it will add the duplicate elements to the end of the container, and return the value It is the tail address after deduplication (it is the address!!) unique() is a function in the C++ standard library function. Its function is to remove adjacent duplicate elements (only one is reserved), so the array needs to be sorted before use. The following A usage of this function has been given. For an array a of length n, unique(a,a+n) - a returns the length of the array after deduplication

#include<iostream>  
#include<cstdio>  
#include<algorithm>  
using namespace std;  
const int N = 100000;  
int a[N+5];  
int main()  
{  
    int n;  
    while (cin>>n)  
    {  
        for (int i = 0;i < n;++i)  
        {  
            scanf("%d",&a[i]);  
        }  
        sort(a,a+n);  
        n = unique(a,a+n) - a;//关键的一句  
        for (int i = 0;i < n;++i)  
        {  
            printf("%d ",a[i]);  
        }  
        puts("");  
    }  
    return 0;  
}  

Code 3: (haven't figured it out yet)

// LeetCode, Remove Duplicates from Sorted Array
// 使用STL,时间复杂度 O(n),空间复杂度 O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return distance(nums.begin(), removeDuplicates(nums.begin(), nums.end(), nums.begin()));
}
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;
}
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398442&siteId=291194637