zoukankan      html  css  js  c++  java
  • Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置

    一:Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    class Solution {
    public:
        int removeElement(vector<int>& nums, int val) {
            int i = 0;
            int n = nums.size();
        
            while(i<n){
                if(nums[i]==val){
                    swap(nums[i],nums[--n]);
                }else{
                    ++i;
                }
            }
    
            nums.erase(nums.begin()+i,nums.end());
            return nums.size();
        }
    };

    二: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.

    For example,
    Given input array nums = [1,1,2],

    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int numsSize = nums.size();
            int j=0;
            int repeat = 0;
            for(int i=0;i<numsSize;i++){
                if(i==0){
                    j=1;
                    repeat = nums[i];
                }else{
                    if(nums[i]!=repeat){
                        nums[j++] = nums[i];
                        repeat = nums[i];
                    }
                }
            }
            for(int i=numsSize-1;i>=j;i--){
                nums.erase(nums.begin()+i);
            }
            return j;
        }
    };

    三: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 1122 and 3. It doesn't matter what you leave beyond the new length.

    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int numsSize = nums.size();
            int j=0;
            int repeat = 0;
            int repeatTime = 0;
            for(int i=0;i<numsSize;i++){
                if(i==0){
                    j=1;
                    repeat = nums[i];
                    repeatTime = 1;
                }else{
                    if(nums[i]!=repeat){
                        nums[j++]=nums[i];
                        repeat = nums[i];
                        repeatTime = 1;
                    }else if(repeatTime < 2){
                        nums[j++]=nums[i];
                        repeatTime ++;
                    }
                }
            }
            for(int i=numsSize-1;i>=j;i--){
                nums.erase(nums.begin()+i);
            }
            return j;
        }
    };
  • 相关阅读:
    极光推送的设备唯一性标识 RegistrationID
    排行榜算法设计实现比较 排序树 平衡二叉树
    UCloud首尔机房整体热迁移是这样炼成的
    from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)
    客户续费模型 逻辑回归 分类器 AdaBoost
    推举算法 AdaBoost 哥德尔奖 Godel Prize
    基于 redis 的分布式锁实现 Distributed locks with Redis debug 排查错误
    Django’s cache framework
    随机森林算法预测法官判决
    时间特征正弦化
  • 原文地址:https://www.cnblogs.com/zengzy/p/4960608.html
Copyright © 2011-2022 走看看