前言
一、力扣 424
check() 函数
代码如下(示例):
class Solution {
public:
int characterReplacement(string s, int k) {
int res = 0; //初始化
int umap[26] = {0}; //字符 ---> 频率
for(int l=0, r=0;r<s.size();++r){
umap[s[r] - 'A']++;
while(!check(umap, k)){
umap[s[l] - 'A']--;
++l; //移动
}
res=max(res,r-l+1);
}
return res;
}
bool check(int map[],int k){
int mx=0, sum=0; // 总的字符 , 最多的字符
for(int i=0;i<26;++i){ //剩余的 是不是 <= k
sum += map[i]; // 是的话, 计算当前的最大值
if(map[i] > mx)
mx=map[i];
}
return sum-mx <= k;
}
};
二、力扣 438
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int>res;
int m=s.size();
int n=p.size();
if(m < n)
return res;
int cnt[26]={0};
int cur[26]={0};
for(const auto&x:p){ //统计p 的频率
cnt[x - 'a']++;
}
for(int i=0;i<n;++i){ //统计s 的前n个频率
cur[s[i] - 'a']++;
}
if(check(cnt, cur))
res.emplace_back(0);
for(int i=n;i<m;++i){
cur[s[i] - 'a']++; //向后移动
cur[s[i-n] - 'a']--;
if(check(cnt ,cur)){
res.emplace_back(i-n+1);
}
}
return res;
}
bool check(int a[], int b[]){
for(int i=0;i<26;++i){
if(a[i] != b[i])
return false;
}
return true;
}
};
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。