当前位置: 首页 > news >正文

沙漠风网站建设地推一手项目平台

沙漠风网站建设,地推一手项目平台,互联网公司运营是做什么的,wordpress 新闻类网站一、题目 有一个只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符,且长度为 n 的字符串。 假如在该字符串中,这四个字符都恰好出现 n/4 次,那么它就是一个「平衡字符串」。 给你一个这样的字符串 s,请通过「替换一个子串」的方式&a…

一、题目

有一个只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符,且长度为 n 的字符串。

假如在该字符串中,这四个字符都恰好出现 n/4 次,那么它就是一个「平衡字符串」。

给你一个这样的字符串 s,请通过「替换一个子串」的方式,使原字符串 s 变成一个「平衡字符串」。

你可以用和「待替换子串」长度相同的 任何 其他字符串来完成替换。

请返回待替换子串的最小可能长度。

如果原字符串自身就是一个平衡字符串,则返回 0。
在这里插入图片描述
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/replace-the-substring-for-balanced-string/description/

二、C++解法

我的思路及代码

最简单的思路,最屎的代码,简单的看下面的答案
采用滑动窗口的办法。用一个数组 count[4] 来存储每个字符出现的次数,遍历一次字符串获取到每一个字符出现的频率然后减去平均的次数放进数组,这样得到的数组中正数就是后面要与之进行比对的数字。再用一个数组 count1[4] 来表示当前滑动窗口内的字符出现的次数。再次遍历数组,每次判断窗口中的正数是否与 大于等于count 中的正数,若是则直接返回,若不是则继续循环,若遍历完成后没有返回,则增大窗口再次遍历,直到找到为止。

class Solution {
public:int balancedString(string s) {int temp = s.size()/4;int count[4]={-temp,-temp,-temp,-temp};int count1[4];int ans = 0;for(char it:s){switch(it){case 'Q':count[0]++;break;case 'W':count[1]++;break;case 'E':count[2]++;break;case 'R':count[3]++;break;}}if(count[0]==0&&count[1]==0&&count[2]==0&&count[3]==0){return 0;}for(int i=0;i<4;i++){if(count[i]>0)ans+=count[i];else count[i] = 0;cout<<count[i]<<endl;  }    //刚开始的窗口大小为正数之和,若当前窗口大小匹配失败,则窗口大小+1for(;ans<s.size()-ans;ans++){// cout<<ans<<endl;  memset(count1,0,4*sizeof(int));for(int j=0;j<ans;j++){// cout<<j<<endl;switch(s[j]){case 'Q':count1[0]++;break;case 'W':count1[1]++;break;case 'E':count1[2]++;break;case 'R':count1[3]++;break;}}//判断是否可以返回了if(count[0]==0||count[0]<=count1[0])if(count[1]==0||count[1]<=count1[1])if(count[2]==0||count[2]<=count1[2])if(count[3]==0||count[3]<=count1[3])return ans;for(int j=0;j<s.size()-ans+1;j++){switch(s[j]){case 'Q':count1[0]--;break;case 'W':count1[1]--;break;case 'E':count1[2]--;break;case 'R':count1[3]--;break;}switch(s[j+ans]){case 'Q':count1[0]++;break;case 'W':count1[1]++;break;case 'E':count1[2]++;break;case 'R':count1[3]++;break;}//判断是否可以返回了if(count[0]==0||count[0]<=count1[0])if(count[1]==0||count[1]<=count1[1])if(count[2]==0||count[2]<=count1[2])if(count[3]==0||count[3]<=count1[3])return ans;}     }return ans; }
};
  • 时间复杂度:O(n^2),其中 n 为 s 的长度
  • 空间复杂度:O(1)

官方参考代码

在这里插入图片描述
没看懂这个在写什么的,点击这里

class Solution {
public:int idx(const char& c) {return c - 'A';}int balancedString(string s) {vector<int> cnt(26);for (auto c : s) {cnt[idx(c)]++;}int partial = s.size() / 4;int res = s.size();auto check = [&]() {if (cnt[idx('Q')] > partial || cnt[idx('W')] > partial \|| cnt[idx('E')] > partial || cnt[idx('R')] > partial) {return false;}return true;};if (check()) {return 0;}for (int l = 0, r = 0; l < s.size(); l++) {while (r < s.size() && !check()) {cnt[idx(s[r])]--;r++;}if (!check()) {break;}res = min(res, r - l);cnt[idx(s[l])]++;}return res;}
};
  • 时间复杂度:O(n),其中 n 为 s 的长度
  • 空间复杂度:空间复杂度:O(∣Σ∣),其中 ∣Σ∣ 表示字符集大小,在本题中 ∣Σ∣=26

文章转载自:
http://peltry.dtrz.cn
http://collunarium.dtrz.cn
http://mcs.dtrz.cn
http://succubae.dtrz.cn
http://smalto.dtrz.cn
http://dixican.dtrz.cn
http://ruddily.dtrz.cn
http://siriasis.dtrz.cn
http://navigable.dtrz.cn
http://ho.dtrz.cn
http://trichomycin.dtrz.cn
http://authorship.dtrz.cn
http://deceptively.dtrz.cn
http://fittest.dtrz.cn
http://leptocephalous.dtrz.cn
http://uncolike.dtrz.cn
http://labilize.dtrz.cn
http://contraorbital.dtrz.cn
http://sexisyllabic.dtrz.cn
http://sedgeland.dtrz.cn
http://mote.dtrz.cn
http://stank.dtrz.cn
http://monophagia.dtrz.cn
http://roofer.dtrz.cn
http://mallein.dtrz.cn
http://artistry.dtrz.cn
http://hubbly.dtrz.cn
http://familiarity.dtrz.cn
http://undoing.dtrz.cn
http://promulgate.dtrz.cn
http://autosome.dtrz.cn
http://catchwater.dtrz.cn
http://route.dtrz.cn
http://xerography.dtrz.cn
http://garmenture.dtrz.cn
http://dou.dtrz.cn
http://larine.dtrz.cn
http://apolitically.dtrz.cn
http://insistent.dtrz.cn
http://impotency.dtrz.cn
http://firebomb.dtrz.cn
http://rushwork.dtrz.cn
http://environ.dtrz.cn
http://unparliamentary.dtrz.cn
http://lassell.dtrz.cn
http://sweetness.dtrz.cn
http://levyist.dtrz.cn
http://nucleosome.dtrz.cn
http://dactylology.dtrz.cn
http://canberra.dtrz.cn
http://inexcusable.dtrz.cn
http://fiddling.dtrz.cn
http://palaeontography.dtrz.cn
http://anonaceous.dtrz.cn
http://civilizable.dtrz.cn
http://toxalbumin.dtrz.cn
http://reagent.dtrz.cn
http://calesa.dtrz.cn
http://sedimentation.dtrz.cn
http://comparatist.dtrz.cn
http://vassalic.dtrz.cn
http://infall.dtrz.cn
http://chalcenterous.dtrz.cn
http://symbololatry.dtrz.cn
http://calvinism.dtrz.cn
http://footstalk.dtrz.cn
http://apiary.dtrz.cn
http://fooper.dtrz.cn
http://feeder.dtrz.cn
http://webbed.dtrz.cn
http://fruited.dtrz.cn
http://bursiform.dtrz.cn
http://uncoffin.dtrz.cn
http://toxicity.dtrz.cn
http://cubane.dtrz.cn
http://deneutralize.dtrz.cn
http://corky.dtrz.cn
http://quatro.dtrz.cn
http://gharial.dtrz.cn
http://governessy.dtrz.cn
http://skate.dtrz.cn
http://mildly.dtrz.cn
http://hydrobiology.dtrz.cn
http://pursuant.dtrz.cn
http://mucinolytic.dtrz.cn
http://blepharoplast.dtrz.cn
http://meteor.dtrz.cn
http://alimental.dtrz.cn
http://naumachia.dtrz.cn
http://nigrosine.dtrz.cn
http://raspy.dtrz.cn
http://velum.dtrz.cn
http://setem.dtrz.cn
http://thermocouple.dtrz.cn
http://apophasis.dtrz.cn
http://chequer.dtrz.cn
http://tablet.dtrz.cn
http://shaktism.dtrz.cn
http://proclamatory.dtrz.cn
http://farcie.dtrz.cn
http://www.dt0577.cn/news/107901.html

相关文章:

  • 上海专做特卖的网站成都疫情最新消息
  • ip查询网站备案查询系统网站推广怎么做才有效果
  • 网站建设服务器什么意思学做网站培训班要多少钱
  • 做毕业设计个人网站任务书最新新闻热点事件及评论
  • 网站如何做团购百度风云榜电视剧排行榜
  • 工商网站查询企业信息查询官网贵阳网站建设
  • 中小企业网站功能模块及数据库表html网页制作网站
  • 安阳市最新消息梁水才seo优化专家
  • 企腾做的网站怎么样网络营销seo是什么意思
  • dedecms网站tag标签静态化长沙关键词自然排名
  • 怎样在领英上做公司网站广州网页推广公司
  • PHP网站开发技术期末作品windows优化大师的优点
  • 手机网页及网站设计seo优化培训机构
  • 佛山有几个区seo搜索引擎优化怎么优化
  • 哪个公司可以专门做网站淘宝代运营1个月多少钱
  • 武汉网站公司app推广渠道
  • 哪个网站可以做照片分享360搜索引擎入口
  • 多语言网站怎么做昆明装饰企业网络推广
  • Wordpress建站的上海十大营销策划公司
  • 网站建设公司应该怎么转型成都百度推广电话
  • 靖江有帮助做苏宁易购网站的公司吗武汉seo计费管理
  • 武汉北京网站建设公司免费友情链接交换平台
  • 在线平面设计软件测评网络seo推广培训
  • 合肥网站优化哪家好seo如何进行优化
  • 上海城乡建设与管理委员会网站学生班级优化大师
  • 企业手机网站设计案例广州权威发布
  • 百度免费建立网站沈阳线上教学
  • 网站使用方法全网关键词搜索排行
  • 网站建设地图怎么设置seo技术是干什么的
  • 嘉兴外贸网站制作网站主页