力扣299场
1.
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.
力扣https://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.
力扣https://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;
}
};