当前位置:网站首页>383. 赎金信
383. 赎金信
2022-04-23 04:15:00 【张.Joshua】
383. 赎金信
- 题号:力扣383
- 知识点:哈希,计数
- 目标完成度:22/150
- 总结
题干:
思路:
- 1.题意解读:判断是否可以用
magazine
里面的字母组成ransonNote
,字母不可重复使用 - 2.首先将
magazine
中的字母和出现次数构建一个字典str1
- 3.然后开始遍历
ransonNote
中的字母,如果遇到magazine
中没有的字母,也就是str1
中不存在的键值,则直接返回False
- 4.每次从
str1
中拿出一个字母组成ransonNote
时,将str1
中的该字母的出现次数减一,当str1
中的字母的出现次数为小于0时,说明magazine
中的字母被重复使用了,返回False
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
str1 = {
}
str2 = {
}
for i in range(len(magazine)):
if magazine[i] in str1:
str1[magazine[i]] += 1
else:
str1[magazine[i]] = 1
for j in range(len(ransomNote)):
if ransomNote[j] in str1:
str1[ransomNote[j]] -= 1
if str1[ransomNote[j]] < 0:
return False
else:
return False
return True
版权声明
本文为[张.Joshua]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44742084/article/details/124356289
边栏推荐
- Set经典小题目
- MYSQL去重方法汇总
- How Zotero quotes in word jump to references / hyperlink
- /etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
- Overview of knowledge map (II)
- 减治思想——二分查找详细总结
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- [string] ranking of country names ----- problem solving notes
- STM32 MCU ADC rule group multi-channel conversion DMA mode
- Alibaba cloud IOT transfer to PostgreSQL database scheme
猜你喜欢
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
[echart] démarrer avec echart
Effects of antibiotics on microbiome and human health
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
The great gods in acmer like mathematics very much
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
随机推荐
Retrieval question answering system baseline
MySQL 2013 lost connection to MySQL server during query
【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
洛谷P1858 【多人背包】 (背包求前k优解)
Basic introduction to spot gold
Hard core chip removal
Chlamydia infection -- causes, symptoms, treatment and Prevention
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
【李宏毅2022 机器学习春】hw6_GAN(不懂..)
Iron and intestinal flora
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
无线充电全国产化电子元件推荐方案
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
Understand the gut organ axis, good gut and good health
国外LEAD,联盟经理常见问答
Alibaba cloud IOT transfer to PostgreSQL database scheme
Mysql---数据读写分离、多实例
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
单片机串口数据处理(1)——串口中断发送数据
【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?