LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间。

C++

献上自己丑陋无比的代码。相当于自己实现一个带计数器的unique函数

class Solution {
public:
    int removeDuplicates(std::vector<int>& nums) {
        if(nums.empty())
            return 0;
        int cnt = 0;
        auto slow = nums.begin();
        auto last = *nums.begin();
        for(auto fast:nums){
            if(cnt == 0) cnt++,slow++;
            else if(cnt == 1){
                if(fast == last) cnt++;
                *slow = fast;
                slow++;
            }
            else {
                if(fast != last) {
                    cnt = 1;
                    *slow = fast;
                    slow++;
                }
            }
            last = fast;
        }
        return distance(nums.begin(),slow);
    }
};

学习标程后写的更简洁的版本,这样的代码扩展性更好:

class Solution {
public:
    int removeDuplicates(std::vector<int>& nums) {
        if(nums.size()<=2) return nums.size();
        auto index = nums.begin()+2;
        for(auto i = nums.begin()+2;i!=nums.end();i++){
            if(*i!=*(index-2)) *index++ = *i;
        }
        return std::distance(nums.begin(),index);
    }
};

再贴一份wiki上找来的std::unique的使用示例。感受一下vector也是可以方便的初始化的。 还有auto的使用(基本操作)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>

int main()
{
    // remove duplicate elements
    std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
    std::sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7
    auto last = std::unique(v.begin(), v.end());
    // v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
    v.erase(last, v.end());
    for (int i : v)
      std::cout << i << " ";
    std::cout << "\n";
}

Java

Python3

原文地址:https://www.cnblogs.com/NeilThang/p/10305847.html

时间: 2024-12-25 16:40:32

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>的相关文章

[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] 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 List II

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

LeetCode(82): Remove Duplicates from Sorted List II

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

LeetCode 80. 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

leetcode 80 Remove Duplicates from Sorted Array II ----- java

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

leetCode 80.Remove Duplicates from Sorted Array II (删除排序数组中的重复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'

[leetcode] 80 Remove Duplicates from Sorted Array II(数组下标操作)

因为这道题目的题意是要求我们在原数组上进行操作,所以操作变得稍微复杂了些,否则直接使用map最为简单. 基本思想是记录两个指针,一个是当前数组,另一个是目的数组,注意如果发现重复数超过2,那么目的数组的cur就要阻塞, 直到不同的出现后再赋值前进. class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0) return 0; int cur=1; //修改后数组的下标点 i

leetcode[80] Remove Duplicates from Sorted Array II

给定一个排好序的数组,要求里面数字重复的次数不超过2,并且记录在原数组的前头,返回剩余长度.例如给定: A = [1,1,1,2,2,3] 返回 5,并且A = [1,1,2,2,3] 思路: 用till记录满足条件的下一个位置,以便下一次填入 用repeat记录重复的次数,超过2则不理,否则往till里记录 如果n为0,则返回0,如果非空,那么第一个数肯定是符合的,所以till从1开始,repeat从1开始. class Solution { public: int removeDuplica