周赛补题

力扣299场

1.

https://leetcode.cn/problems/check-if-matrix-is-x-matrix/icon-default.png?t=M5H6https://leetcode.cn/problems/check-if-matrix-is-x-matrix/
先找规律,然后遍历

class Solution {
public:
    bool checkXMatrix(vector<vector<int>>& grid) {
        
        for(int i=0;i<grid.size();i++){
            for(int j=0;j<grid.size();j++){
                if(i==j||(i+j+1)==grid.size()){ if(grid[i][j]==0)return false;}
                else{ if(grid[i][j]!=0) return false;}
            }
        }
        
        return true;
    }
};

2.

力扣icon-default.png?t=M5H6https://leetcode.cn/problems/count-number-of-ways-to-place-houses/

 f[i]=f[i−1]+f[i−2],正好是斐波拉切数列斐波拉切数列

const int MO = 1e9 + 7;
class Solution {
public:
    int countHousePlacements(int n) {
        vector<long long> f(n + 1);
        f[1] = 2;
        if (n == 1) return f[n] * f[n];
        f[2] = 3;
        if (n == 2) return f[n] * f[n];
        for (int i = 3; i <= n; ++i) {
            f[i] = (f[i - 1] + f[i - 2]) % MO;
        }
        return f[n] * f[n] % MO;
    }
};

力扣81场双周

1.

力扣icon-default.png?t=M5H6https://leetcode.cn/problems/count-asterisks/找到要求的两个“|”,再计数

class Solution {
public:
    int countAsterisks(string s) {
        int ct=0,ln=0,ans=0;
            
        for(auto c:s){
            if(c=='|'){
                ln++;
                if(ln==2) {
                    ln=0;
                    // ans+=ct;
                    ct=0;
                }else{
                    ans+=ct;
                    ct=0;
                }
            }
            else if(c=='*') ct++;
        }
        
        return ans + ct;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值