第80场双周赛

这篇博客涵盖了多种算法的应用,包括检查强密码的实现,统计得分小于K的子数组数目,以及在给定字符映射下判断字符串是否匹配。这些算法涉及到字符串处理、排序、哈希表和双指针等技术,展示了在实际问题中的解决方案。
摘要由CSDN通过智能技术生成

强密码检验器 II

模拟

class Solution {
public:
    bool strongPasswordCheckerII(string s) {
        string str="!@#$%^&*()-+";
        bool a=false,b=false,c=false,d=false;
        if(s.size()<8) return false;
        for(int i=0;i<s.size()-1;i++)
        {
            if(s[i]==s[i+1]) return false;
        }
        for(int i=0;i<s.size();i++)
        {
            if(islower(s[i])) a=true;
            else if(isupper(s[i])) b=true;
            else if(isdigit(s[i])) c=true;
            else if(str.find(to_string(s[i]))) d=true;
        }
        if(a&&b&&c&&d) return true;
        return false;

    }
};

咒语和药水的成功对数

排序+二分

class Solution {
public:
    vector<int> successfulPairs(vector<int>& a, vector<int>& b, long long s) {
        vector<int>res;
        int cnt=0;
         sort(b.begin(),b.end());
         for(auto&x:a)
         {
             int cnt=b.end()-lower_bound(b.begin(),b.end(),(double)s/(double)x);
             res.push_back(cnt);
         }
         return res;
    }
};

替换字符后匹配

哈希表+模拟

class Solution {
public:
    bool matchReplacement(string a, string b, vector<vector<char>>& mappings) {
        unordered_map<char,set<char>>hash;
        for(auto&p:mappings)
        {
            int x1=p[0];
            int x2=p[1];
            hash[x1].insert(x2);
        }
        int n=a.size();
        int m=b.size();
        for(int i=0;i+m-1<n;i++)
        {
            bool flag=true;
            for(int j=0;j<m;j++)
            {
                if(a[i+j]!=b[j]&&!hash[b[j]].count(a[i+j]))
                {
                    flag=false;
                    break;
                }
            }
            if(flag) return true;
        }
        return false;
    }
};

统计得分小于 K 的子数组数目

前缀和+双指针

class Solution {
public:
    long long countSubarrays(vector<int>& nums, long long k) {
        int n=nums.size();
        vector<long long>s(n+1);
        for(int i=1;i<=n;i++)
        {
            s[i]=nums[i-1]+s[i-1];
        }
        long long res=0;
        for(int i=1,j=0;i<=nums.size();i++)
        {
            while((s[i]-s[j])*(i-j)>=k) j++;
            res+=i-j;
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值