力扣285场
第一题
力扣https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/
遍历+数组计数
class Solution {
public:
int A[27],a[27];
string greatestLetter(string s) {
for(int i=0;i<=26;i++){A[i]=0,a[i]=0;}
for(char ch:s){
if(ch < 'a') A[ch-'A']++;
else a[ch-'a']++;
}
string ss="";
for(int i = 25;i>=0;i--){
if(A[i]&&a[i]) {ss.push_back('A'+i);return ss;}
}
return "";
}
};
也可以使用哈希表来处理
class Solution{
public:
string greattestLetter(string s){
unordered_set<char> S;
for(auto c:s)S.insert(c);
for(char i='z';i>='a';i--)
{
if(S.count(i)&&S.count(i-32)){
return string(1,i-32);
}
}
}
return "";
}
第二题
力扣https://leetcode.cn/problems/sum-of-numbers-with-units-digit-k/每个数都可以表示成10的倍数以上k的形式,且num-nk一定是10的倍数
class Solution {
public:
int minimumNumbers(int num, int k) {
if (num == 0) return 0;
for (int n = 1; n <= 10 && num - k * n >= 0; ++n)
if ((num - k * n) % 10 == 0) return n;
return -1;
}
};
Acwing56场
第一题
4482. 分组 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/4485/用数组统计每个数出现次数,取最大值得出答案
#include <bits/stdc++.h>
using namespace std;
const int N=110;
int n;
int a[N];
int main
{
cin>>n;
int res=0;
while(n--)
{
int x;
cin>>x;
res=max(res,++cnt[x]);
}
cout<<res;
}