Leetcoce 1566. Detect Pattern of Length M Repeated K or More Times两种方法

1

class Solution {
public:
    bool containsPattern(vector<int>& arr, int m, int k) {
        // 遍历起点,配置pattern 直接跑k个看是否符合,不符合就结束
        int cnt = 0;
        string arr2;
        for(auto& s:arr)
            arr2+=to_string(s);
        for(int i=0; i<arr2.size()-1; i++){
            string pattern;
            pattern = arr2.substr(i,m);
            cout<<pattern<<endl;
            for(int j=i;j<arr2.size(); j=j+m){
                string tmp;
                tmp = arr2.substr(j, m);
                cout<<"tmp"<<tmp<<endl;
                if(tmp.compare(pattern)==0){
                    cnt++;
                    if(cnt==k) return true;
                }else{
                    break;
                }
            }
            cnt=0;
        }
        return false;
    }
};

2

class Solution {
public:
    bool patterns(vector<int>& arr, int offset, int pattern_length, int k){
        for(int i=1; i<k; i++){
            for(int j=0; j<pattern_length; j++){
                if(arr[offset+i*pattern_length+j]!=arr[offset+j]) return false;
            }
        }
        return true;
    }
    
    
    bool containsPattern(vector<int>& arr, int m, int k) {
        int n=arr.size();
        if(n<m*k) return false;
        for(int i=0; i+m*k<=n; i++){
                               
            if(patterns(arr, i, m, k)) return true;
        }
        return false;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值