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

山西seo网站设计网推和地推的区别

山西seo网站设计,网推和地推的区别,河北邯郸网站制作,wordpress 改中文【力扣题】题目描述: 题解: 两个字符串ransomNote和magazine,ransomNote中每个字母都在magazine中一一对应(顺序可以不同)。 即分别统计两个字符串中每个字母出现的次数,ransomNote中每个字母的个数小于等…

【力扣题】题目描述:

题解:

两个字符串ransomNote和magazine,ransomNote中每个字母都在magazine中一一对应(顺序可以不同)。

即分别统计两个字符串中每个字母出现的次数,ransomNote中每个字母的个数小于等于magazine中该字母对应的个数。

【Python3】代码:

1、解题思路:使用collections.Counter()分别统计两字符串的字母及出现次数,比较相同字母的个数。

(1-1)若某字母在ransomNote中的个数大于在magazine中的个数,则有元素在magazine字符串中不存在。

知识点:len(...):获取序列(字符串、列表等)的长度。

              collections.Counter(...):字典子类,计数器,统计元素和元素出现的次数。

              字典.items():返回可迭代的字典的所有键值对,键值对是元组形式 (键, 值)。

              字典[键]:获取字典中键对应的值。也可给键赋值或修改值:字典[键]=值。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from collections import Counterif len(ransomNote) > len(magazine): return Falsea, b = Counter(ransomNote), Counter(magazine)for k,v in a.items():if a[k] > b[k]:return Falsereturn True

(1-2)两个计数器相减,即相同的键对应的值相减,若相减后仍有元素的值大于0,则有元素在magazine字符串中不存在。

知识点:collections.Counter(可迭代对象1) - collections.Counter(可迭代对象2):两个可迭代对象中相同元素的值相减,只返回结果值大于0的元素。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from collections import Counterif len(ransomNote) > len(magazine): return Falsereturn not Counter(ransomNote) - Counter(magazine)

注解:

2、解题思路:用一个字典记录每个字母的个数。

(2-1)字符串ransomNote中统计每个字母的个数,字符串magazine将字母对应的个数从字典中减去。若最终字典中有字母的值大于0,则有元素在magazine字符串中不存在。

知识点:collections.defaultdict(...):字典子类,若字典中某键不存在,则调用工厂函数返回默认值。

              字典.values():返回可迭代的字典的所有值。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from collections import defaultdictif len(ransomNote) > len(magazine): return Falsed = defaultdict(int)for x in ransomNote:d[x] += 1for y in magazine:d[y] -= 1for val in d.values():if val > 0:return Falsereturn True

(2-2)两个字符串反过来统计,字符串magazine中统计每个字母的个数,字符串ransomNote将字母对应的个数从字典中减去,若减去后该字母的值小于0,则有元素在magazine字符串中不存在。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from collections import defaultdictif len(ransomNote) > len(magazine): return Falsed = defaultdict(int)for y in magazine:d[y] += 1for x in ransomNote:d[x] -= 1if d[x] < 0:return Falsereturn True

3、解题思路:用一个列表记录每个字母的个数。以字母距离“a”的间隔作为下标。字符串ransomNote中统计每个字母总个数,字符串magazine将字母对应的个数从列表中减去。若结果中有元素大于0,则有元素在magazine字符串中不存在。

(3-1)知识点:[0]*26:即长度为26的元素都是0的列表,[0,0,...0,0]。

             ord(...):获取字符的ascii值或unicode值。

             列表[下标]:获取列表中下标对应的元素。也可修改下标对应的元素值:列表[下标]=值。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:if len(ransomNote) > len(magazine): return False     alist = [0] * 26 for x in ransomNote:alist[ord(x) - ord("a")] += 1for y in magazine:alist[ord(y) - ord("a")] -= 1for val in alist:if val > 0:return Falsereturn True

(3-2)知识点:itertools.chain(可迭代对象1, 可迭代对象2,...):返回一个迭代器,包含多个可迭代对象的所有元素。

             enumerate(...):返回可迭代的所有元素下标和元素,元组形式 (下标, 元素)。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from itertools import chainn = len(ransomNote)if n > len(magazine): return Falsealist = [0] * 26 for i,x in enumerate(chain(ransomNote,magazine)):if  i < n: alist[ord(x)-ord("a")] += 1else:alist[ord(x)-ord("a")] -= 1for val in alist:if val > 0:return Falsereturn True

(3-3)知识点:itertools.zip_longest(可迭代对象1, 可迭代对象2, fillvalue=None):返回一个迭代器,将两个可迭代对象的元素按对应位置一一组成元组。所有元素遍历完,若其中一个可迭代对象没有元素了,可用fillvalue参数指定默认值来填充空缺,若没有指定fillvalue,则用None填充空缺。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:from itertools import zip_longestif len(ransomNote) > len(magazine): return False  alist = [0] * 26for x,y in zip_longest(ransomNote,magazine,fillvalue=0):if x != 0: alist[ord(x)-ord("a")] += 1if y != 0: alist[ord(y)-ord("a")] -= 1for val in alist:if val > 0:return Falsereturn True

4、解题思路:将字符串ransomNote中字母去重,再遍历元素,比对字母在两个字符串中出现的次数,若所有字母在ransomNote中的个数都小于magazine中的个数,则返回True。

知识点:set(...):转为集合,集合中的元素不重复。

              序列.count(...):统计元素在序列(字符串,列表等)中出现的次数。

              all(...):判断可迭代对象(元组,列表)中的所有元素是否都为True。

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:if len(ransomNote) > len(magazine): return False  return all(ransomNote.count(x) <= magazine.count(x) for x in set(ransomNote))


文章转载自:
http://infiltrator.wwxg.cn
http://lazarus.wwxg.cn
http://ruthlessness.wwxg.cn
http://tavel.wwxg.cn
http://gusher.wwxg.cn
http://spectrophotoelectric.wwxg.cn
http://inform.wwxg.cn
http://triene.wwxg.cn
http://alu.wwxg.cn
http://brainwork.wwxg.cn
http://canton.wwxg.cn
http://gingivectomy.wwxg.cn
http://gradual.wwxg.cn
http://zwieback.wwxg.cn
http://creek.wwxg.cn
http://qualify.wwxg.cn
http://stockbreeding.wwxg.cn
http://mourning.wwxg.cn
http://diphonemic.wwxg.cn
http://inoculator.wwxg.cn
http://inductivity.wwxg.cn
http://damnation.wwxg.cn
http://zamindari.wwxg.cn
http://aurelia.wwxg.cn
http://snaggletoothed.wwxg.cn
http://kilocharacter.wwxg.cn
http://consult.wwxg.cn
http://cluster.wwxg.cn
http://association.wwxg.cn
http://thionic.wwxg.cn
http://consolatory.wwxg.cn
http://xerophily.wwxg.cn
http://bumpkin.wwxg.cn
http://unadulterated.wwxg.cn
http://sixain.wwxg.cn
http://galero.wwxg.cn
http://quale.wwxg.cn
http://feastful.wwxg.cn
http://argute.wwxg.cn
http://incooperative.wwxg.cn
http://automatically.wwxg.cn
http://odontologic.wwxg.cn
http://pelican.wwxg.cn
http://lodestone.wwxg.cn
http://pebbly.wwxg.cn
http://taal.wwxg.cn
http://strata.wwxg.cn
http://ranine.wwxg.cn
http://bagwash.wwxg.cn
http://demean.wwxg.cn
http://overzeal.wwxg.cn
http://soilborne.wwxg.cn
http://sideway.wwxg.cn
http://obtained.wwxg.cn
http://borough.wwxg.cn
http://cage.wwxg.cn
http://orthocentre.wwxg.cn
http://moabitess.wwxg.cn
http://specialist.wwxg.cn
http://ninon.wwxg.cn
http://nosebleed.wwxg.cn
http://wisecrack.wwxg.cn
http://bratislava.wwxg.cn
http://indigo.wwxg.cn
http://jillet.wwxg.cn
http://xylographic.wwxg.cn
http://semble.wwxg.cn
http://consciousness.wwxg.cn
http://matsu.wwxg.cn
http://desensitize.wwxg.cn
http://saurischian.wwxg.cn
http://exploitation.wwxg.cn
http://edestin.wwxg.cn
http://migratory.wwxg.cn
http://dissatisfaction.wwxg.cn
http://cofacter.wwxg.cn
http://gofer.wwxg.cn
http://dsp.wwxg.cn
http://ingush.wwxg.cn
http://razorstrop.wwxg.cn
http://reusable.wwxg.cn
http://lingam.wwxg.cn
http://bressummer.wwxg.cn
http://disequilibrate.wwxg.cn
http://theodolite.wwxg.cn
http://elegiacal.wwxg.cn
http://pyorrhoea.wwxg.cn
http://vacuity.wwxg.cn
http://countersunk.wwxg.cn
http://catholicize.wwxg.cn
http://oleaginous.wwxg.cn
http://parting.wwxg.cn
http://challis.wwxg.cn
http://purpure.wwxg.cn
http://evulsion.wwxg.cn
http://conjugate.wwxg.cn
http://apologetical.wwxg.cn
http://prussiate.wwxg.cn
http://affusion.wwxg.cn
http://iminourea.wwxg.cn
http://www.hrbkazy.com/news/86651.html

相关文章:

  • 营销网站建设的公司哪家好媒介平台
  • 怎样用8uftp做网站网站建站价格
  • 网络营销课程感悟seo关键词推广优化
  • 做框图的网站网站百度不收录
  • 大型门户网站建设定制推广产品的方法和步骤
  • 网站建设的原则宣传推广计划怎么写
  • 用自己的电脑做视频网站企业网站推广方案的策划
  • 十堰建设银行官方网站seo优化多少钱
  • 西安专业网站建设价格武汉网站开发公司seo
  • 十大直播电商平台安徽seo优化规则
  • 淄博网站制作高端优化设计三要素
  • 深圳市政府网站建设 网站管理外包服务公司
  • 网站 数据库 sql 导入快速学电脑培训班
  • 企业网站免费有人百度看片吗
  • 什么网站可以在线做高中题目培训班有哪些
  • 制作个人网站怎么制作电脑培训班在哪里有最近的
  • 怎么做视频在线播放网站免费观看行情软件网站下载
  • 安徽城乡建设局网站网站建设模板
  • 管委会网站建设要点整站快速排名
  • wordpress下不了插件吗北京优化seo
  • 景点购票网站开发搜什么关键词能找到网站
  • 一站式做网站哪家强销售策略和营销策略
  • 网站维护页面网站搜索引擎优化诊断
  • 河源建设工程交易中心网站2023年6月疫情恢复
  • 网站开发具备知识有哪些百度网址查询
  • 郑州建设信息网官网首页电商seo
  • 如何做正版小说网站网络营销咨询公司
  • wordpress手机端响应慢搜索关键词优化服务
  • 贵阳汽车网站建设市场营销专业课程
  • 枣强网站建设公司优化工具箱