Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

 

Code:

复制代码
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        if(n<3) return n;
        int i=0;
        bool detect=false;
        for(int j=1;j<n;j++){
            if(A[j]!=A[i]||detect==false){
                if(A[j]!=A[i])
                    detect=false;
                else
                    detect=true;
                A[++i]=A[j];
            }
        }
        return i+1;
    }
};
复制代码

 

posted @   WinsCoder  阅读(138)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
阅读排行:
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· 一文搞懂MCP协议与Function Call的区别
· 如何不购买域名在云服务器上搭建HTTPS服务
点击右上角即可分享
微信分享提示