313场 单周 竞赛 - 力扣 (LeetCode)
1.力扣
两数最小值来限制长度,最后判断最大数能否整除最小数即可
class Solution {
public:
int commonFactors(int a, int b) {
int ans=0;
int k=min(a,b);
int n=max(a,b);
for(int i=1;i<=k;i++)
{
if(k%i==0&&n%i==0)ans++;
}
return ans;
}
};
2.力扣
直接枚举,选定选定沙漏中心位置(i,j),再按题目要求求出沙漏中数的大小,最后res一个一个比较
class Solution {
public:
int maxSum(vector<vector<int>>& grid) {
int n=grid.size(),m=grid[0].size();
int res=0;
for(int i=1;i<n-1;i++)
for(int j=1;j<m-1;j++)
{
int s=grid[i][j];
for(int k=j-1;k<=j+1;k++)
s+=grid[i-1][k]+grid[i+1][k];
res=max(res,s);
}
return res;
}
};
88场 双周 竞赛 - 力扣 (LeetCode)
1.力扣
统计每个字母出现次数,枚举删除第一个字母,检查剩下字母出现频率是否相同
class Solution {
public:
bool equalFrequency(string word) {
int cnt[26] = {0};
int j;
for (auto &ch : word) {
cnt[ch - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] == 0) {
continue;
}
cnt[i]--;
int tmpCnt = 0;
for (j = 0; j < 26; j++) {
if (cnt[j] > 0) {
if (tmpCnt == 0) {
tmpCnt = cnt[j];
} else if (cnt[j] != tmpCnt) {
break;
}
}
}
if (j == 26) {
return true;
}
cnt[i]++;
}
return false;
}
};
2.力扣
使用set维护[1,n+1],更新元素则删掉当前video,上传时, 为该区间最小值减1
class LUPrefix {
set<int> st;
public:
LUPrefix(int n) {
for (int i = 1; i <= n + 1; i++) {
st.insert(i);
}
}
void upload(int video) {
st.erase(video);
}
int longest() {
return *st.begin() - 1;
}
};