80. Remove Duplicates from Sorted Array II

80. Remove Duplicates from Sorted Array II

大家好,又见面了,我是你们的朋友全栈君。

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 matter what you leave beyond the new length. 

解析

方法一:很灵活的方法,扩展性较强,如果将occur<2 改为   occur<3  ,就变成了允许重复最多三次
    
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        if(n<=2) return n;
        int index=2;//允许重复两次,可以修改为三次
        for(int i=2;i<n;i++)
        {
            if(A[i]!=A[index-2])//允许重复两次,可以修改为三次
                A[index++]=A[i];
        }
         
        return index;
    }
};

方法二:简洁版本

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        int index=0;
        for(int i=0;i<n;i++){
            if(i>0&&i<n-1&&A[i]==A[i-1]&&A[i]==A[i+1])
                continue;
             
            A[index++]=A[i];
        }
         
        return index;
    }
};
// 80. Remove Duplicates from Sorted Array II
class Solution_80 {
public:
    int removeDuplicates_(vector<int>& nums) {

        if (nums.size()<=1)
        {
            return nums.size();
        }

        int len = 0;
        int start = 0, end = 0;

        for (int i = 1; i < nums.size();i++)
        {
            if (nums[i]==nums[i-1])
            {
                end++;
            }
            else
            {
                start = i;
                end = i;
            }
            if (end-start+1<=2)
            {
                nums[++len] = nums[i];
            }
        }
        
        return len+1;
    }

    int removeDuplicates(int A[], int n) {
        
        vector<int> vec(A, A + n); //vec传值不能达到A;
        return removeDuplicates_(vec);
    }

    int removeDuplicates_1(int A[], int n) {
        int *nums = A;
        if (n <= 1)
        {
            return n;
        }

        int len = 0;
        int start = 0, end = 0;

        for (int i = 1; i < n; i++)
        {
            if (nums[i] == nums[i - 1])
            {
                end++;
            }
            else
            {
                start = i;
                end = i;
            }
            if (end - start + 1 <= 2)
            {
                nums[++len] = nums[i];
            }
        }

        return len + 1;
    }


};

题目来源

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107599.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • intellij idea激活码【2021.8最新】

    (intellij idea激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~S32P…

    2022年3月26日
    50
  • iic电平转换电路_光耦电平转换电路图

    iic电平转换电路_光耦电平转换电路图

    2022年8月10日
    8
  • GridData

    GridDataGridLayout//容器下面元素的列数,makeColumnsEqualWidth是否相同大小单元格publicGridLayout(intnumColumns,booleanmakeColumnsEqualWidth);GridData类可以与GridLayout类配合使用,其中构造函数有:[code="java"]publicGridData();…

    2022年5月9日
    78
  • kali如何更换源(怎样换一个kali源)

    KaliLinux的换源和更新1.修改源文件(需要用root权限)[plain]viewplaincopyvim /etc/apt/sources.list  2.这里修改两个我认为还好的源,因为每个地方不同,选择源的时候建议使用一些常用的吧。比如:阿里云源,中科大源之类的官方源更新的速度太慢了,所以我注释掉了,只使用两

    2022年4月12日
    159
  • navicat生成激活码时出错【最新永久激活】2022.02.21[通俗易懂]

    (navicat生成激活码时出错)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlHCIQ56F36O-eyJsaWNlbnNlSWQi…

    2022年4月1日
    190
  • Excel宏教程 (宏的介绍与基本使用)

    Excel宏教程 (宏的介绍与基本使用)Excel宏教程(宏的介绍与基本使用)Microsoftexcel是一款功能非常强大的电子表格软件。它可以轻松地完成数据的各类数学运算,并用各种二维或三维图形形象地表示出来,从而大大简化了数据的处理工作。但若仅利用excel的常用功能来处理较复杂的数据,可能仍需进行大量的人工操作。但excel的强大远远超过人们的想象–宏的引入使其具有了无限的扩展性,因而可以很好地解决复杂

    2022年5月6日
    1.0K

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号