【LeetCode】第 373 场周赛题解

54 阅读1分钟

100139. 循环移位后的矩阵相似检查

题解:模拟。

class Solution {
public:
    bool areSimilar(vector<vector<int>>& mat, int k) {
        int n = mat.size(), m = mat[0].size();
        vector<vector<int>> res(n, vector<int>(m));
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(i%2 == 0){
                    res[i][j] = mat[i][((j-k+m)%m+m)%m];
                }else{
                    res[i][j] = mat[i][(j+k)%m];
                }
            }
        }
        return res == mat;
    }
};

100134. 统计美丽子字符串 I

题解:前缀和

class Solution {
public:
    int a[10001];
    int b[10001];
    bool check(char c){
        if(c =='a' || c == 'e' || c =='i' || c =='o' || c =='u'){
            return true;
        }
        return false;
    }
    int beautifulSubstrings(string s, int k) {
        int n = s.size();
        s = "0"+s;
       
        for(int i=1; i<=n; i++){
            if(check(s[i])){
                a[i] = a[i-1] +1;
                b[i] = b[i-1];
            }else{
                a[i] = a[i-1];
                b[i] = b[i-1]+1; 
            }
        }
        int res = 0;
        for(int i=2; i<=n; i++){
            for(int j=1; j<=i; j++){
                int g = a[i] - a[j-1];
                int h = b[i] - b[j-1];
                if(g == h && (g*h)%k == 0){
                    res++;
                }
            }
        }
        return res;
    }
};

100142. 交换得到字典序最小的数组

题解:分组排序

class Solution {
public:
    vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {
        int n = nums.size();
        vector<pair<int, int>> f(n);
        for(int i=0; i<n; i++){
            f[i] = {nums[i], i};
        }
        sort(f.begin(), f.end());
        vector<int> res(n);
        int i=0;
        while(i < n){
            int j = i;
            i++;
            while(i < n && f[i].first - f[i-1].first<=limit){
                i++;
            }
            vector<int> p;
            for(int k=j; k<i; k++){
                p.push_back(f[k].second);
            }
            sort(p.begin(), p.end());
            for(int k=0; k<p.size(); k++){
                res[p[k]] = f[j++].first;
            }
        }
        return res;
    }
};