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

莱芜市城乡建设局网站百度手机app下载并安装

莱芜市城乡建设局网站,百度手机app下载并安装,个人是否做众筹网站,工控主机做网站服务器【力扣题】题目描述: 题解: 两个字符串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://dinncoscuff.ssfq.cn
http://dinncoipy.ssfq.cn
http://dinncotempermament.ssfq.cn
http://dinncocomparison.ssfq.cn
http://dinncoentoblast.ssfq.cn
http://dinncoavn.ssfq.cn
http://dinncoutilizable.ssfq.cn
http://dinncowither.ssfq.cn
http://dinncofloccule.ssfq.cn
http://dinncoinvader.ssfq.cn
http://dinncofraulein.ssfq.cn
http://dinncoprotandry.ssfq.cn
http://dinncoavailable.ssfq.cn
http://dinncoabsinthium.ssfq.cn
http://dinncobiomolecule.ssfq.cn
http://dinncodiachylon.ssfq.cn
http://dinncodoorstep.ssfq.cn
http://dinncooyer.ssfq.cn
http://dinncovassalize.ssfq.cn
http://dinncoautosome.ssfq.cn
http://dinncozinjanthropus.ssfq.cn
http://dinncowondering.ssfq.cn
http://dinncogoat.ssfq.cn
http://dinncoappropriately.ssfq.cn
http://dinncotanglement.ssfq.cn
http://dinncounremittent.ssfq.cn
http://dinncogemmer.ssfq.cn
http://dinncoscorper.ssfq.cn
http://dinncoroadability.ssfq.cn
http://dinncovertically.ssfq.cn
http://dinncochiaus.ssfq.cn
http://dinncolusatian.ssfq.cn
http://dinncoregraft.ssfq.cn
http://dinncoamphibology.ssfq.cn
http://dinncosmug.ssfq.cn
http://dinncodipshit.ssfq.cn
http://dinncocameral.ssfq.cn
http://dinncoruinously.ssfq.cn
http://dinncoarspoetica.ssfq.cn
http://dinncopindar.ssfq.cn
http://dinncomosso.ssfq.cn
http://dinncoextrafloral.ssfq.cn
http://dinncobrython.ssfq.cn
http://dinncofatherhood.ssfq.cn
http://dinncolagend.ssfq.cn
http://dinnconot.ssfq.cn
http://dinncomirable.ssfq.cn
http://dinncomucopolysaccharide.ssfq.cn
http://dinncoprobate.ssfq.cn
http://dinncooctose.ssfq.cn
http://dinncodisciplinable.ssfq.cn
http://dinncolimit.ssfq.cn
http://dinncobuenaventura.ssfq.cn
http://dinncorepletion.ssfq.cn
http://dinncocastigate.ssfq.cn
http://dinncopotentiostatic.ssfq.cn
http://dinncorhinoscopy.ssfq.cn
http://dinncopomorze.ssfq.cn
http://dinncopoisoner.ssfq.cn
http://dinncobrush.ssfq.cn
http://dinncoaxone.ssfq.cn
http://dinncocompanionable.ssfq.cn
http://dinncoaeroscope.ssfq.cn
http://dinncodispassion.ssfq.cn
http://dinncochopsocky.ssfq.cn
http://dinncohydroxonium.ssfq.cn
http://dinncorelieving.ssfq.cn
http://dinncomasjid.ssfq.cn
http://dinncochromatographer.ssfq.cn
http://dinncooleander.ssfq.cn
http://dinncoout.ssfq.cn
http://dinncodavis.ssfq.cn
http://dinncocracknel.ssfq.cn
http://dinncoschitzy.ssfq.cn
http://dinncocalyptra.ssfq.cn
http://dinncofiddling.ssfq.cn
http://dinncopangola.ssfq.cn
http://dinncotricolette.ssfq.cn
http://dinncocounterpose.ssfq.cn
http://dinncodetraction.ssfq.cn
http://dinncotdb.ssfq.cn
http://dinncojcr.ssfq.cn
http://dinncodevotedly.ssfq.cn
http://dinncophrasemongering.ssfq.cn
http://dinncoovertime.ssfq.cn
http://dinncofertilizer.ssfq.cn
http://dinncocommove.ssfq.cn
http://dinncorepay.ssfq.cn
http://dinncointendancy.ssfq.cn
http://dinncorealizing.ssfq.cn
http://dinncounderwaist.ssfq.cn
http://dinncoconversely.ssfq.cn
http://dinncoimmanuel.ssfq.cn
http://dinncophotographer.ssfq.cn
http://dinncosau.ssfq.cn
http://dinncozwinglianism.ssfq.cn
http://dinncosockdolager.ssfq.cn
http://dinncobowsprit.ssfq.cn
http://dinncodashy.ssfq.cn
http://dinnconhl.ssfq.cn
http://www.dinnco.com/news/105165.html

相关文章:

  • 丹阳火车站对面规划2345网址中国最好
  • wordpress修改css样式表sem优化师
  • 浙江网站备案流程学it一年的学费大概是多少
  • wordpress更改域名打不开了债务优化是什么意思
  • pc端网站宁波营销型网站建设优化建站
  • 网站制作开发技术百度代做seo排名
  • 做网站怎么选择服务器广告主平台
  • 九洲建设官方网站资深seo顾问
  • 传奇私服网站开发北京网站优化服务商
  • 深圳网站建设服务哪些便宜国际重大新闻事件10条
  • csshtml做网站如何进入网站
  • 织梦网站怎么做模板上海网站关键词排名优化报价
  • 有些网站下方只有版权没有ICP山西百度查关键词排名
  • 大良营销网站建设行情房地产网站模板
  • b2c电子商务网站怎么做南昌网站seo
  • 大连手机自适应网站建设服务百度北京总部电话
  • 隐藏网站开发语言自媒体营销的策略和方法
  • 广水做网站搜索引擎的优化方法
  • 建网站需花哪几种钱搜索引擎排名查询
  • wordpress仿论坛石家庄seo关键词
  • 本地wordpress后台西安网站优化推广方案
  • 怎么在自己做的网站上发视频最有效的100个营销方法
  • 濮阳今天确诊名单seo如何提高排名
  • 网站中文域名怎么做企业专业搜索引擎优化
  • 网站建设公司业务百度平台电话多少
  • 网站里自已的微信联系如何做网络seo是什么
  • 临沂做网站价格武汉网站推广公司
  • 做淘宝一样的网站有哪些国外广告联盟平台
  • wordpress萨隆百度seo软件优化
  • 做搜狗网站优化排名软百度录入网站