当前位置:网站首页>leetcode771. Gemstones and stones
leetcode771. Gemstones and stones
2022-04-23 01:28:00 【2021dragon】

LeetCode Series articles
List of articles
One 、 Title Description
Give you a string j e w e l s jewels jewels Represents the type of gem in stone , Another string s t o n e s stones stones Represents the stone you have . s t o n e s stones stones Each character in represents a type of stone you have , You want to know how many stones you have are gems .
Be careful :
- j e w e l s jewels jewels and s t o n e s stones stones It consists of only English letters .
- j e w e l s jewels jewels All characters in are unique .
- Letters are case sensitive , therefore a a a and A A A It's different types of stones .
Two 、 Example
Input : jewels = “aA”, stones = “aAAbbbb”
Output : 3
3、 ... and 、 Main idea
The meaning of the question is very clear , Is to count the number of gemstones in the stone . We can choose to traverse the string directly s t o n e s stones stones, Judge whether each stone is a gem in turn , If so, include it in the count , Finally, you can get the number of gemstones . We can also traverse the string first s t o n e s stones stones, Count the number of occurrences of each stone , And then traverse the string j e w e l s jewels jewels, Judge whether each gem has appeared in the stone in turn , If so, add the number of occurrences of the stone to the count , Finally, you can get the number of gemstones .
Four 、 Code implementation
1、 utilize unordered_set Containers
If we are directly traversing the string s t o n e s stones stones Judge whether each stone is a gem , Finally get the number of gemstones , Then we can store the type of gem in unordered_set The container , When judging whether a stone is a gem, you can unordered_set Look in the container , If found, it means that the stone is a gem .
The code is as follows :
2、 utilize unordered_map Containers
If we traverse first s t o n e s stones stones character string , Count the number of occurrences of each stone , At this point, we can store the stone and its occurrence times in unordered_map The container , And then traverse the string j e w e l s jewels jewels, If j e w e l s jewels jewels A character in a string is in s t o n e s stones stones There has been , The character is placed in s t o n e s stones stones The number of occurrences in adds up to c o u n t count count among , Finally, you can get the number of gemstones .
The code is as follows :
版权声明
本文为[2021dragon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220957597561.html
边栏推荐
- Itextsharp infrastructure
- How to introduce SPI into a project
- DFS parity pruning
- Software maintenance exercises
- Slow response of analysis API
- Interface automation session authentication solution
- App uses the template message from WeChat official account for message push.
- “自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
- VSCODE + PHP Debug + 名字空间指引
- Generating class diagram with EA reverse engineering code
猜你喜欢

智能手表的下半场,机遇与挑战并存

10月24号是什么?中国的高级程序员为什么没有五六十岁的,而国外人、、、,玉念聿辉带你过1024程序员节日

清研环境深交所上市:年营收1.8亿 市值41亿

Redis forgets the password and resets the password

engine. Post() handles post requests

VS+C# 实现窗体输入框默认显示灰色文字

API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)

2022 penetration job interview (thinking)

DFS奇偶性剪枝

引爆炸弹(DFS)
随机推荐
[蓝桥杯][2019年第十届真题]外卖店优先级
GBase 8s查询处理和优化
计蒜客:数独(DFS)
代码实现发邮件---sendemails
Analysis and configuration process of epit periodic timer for imx6ull bare metal development
Gbase 8s 共享内存段删除
Gbase 8t index
计蒜客家谱(dfs求直系后代数)
Zhang Jian of Huawei cloud IOT expert group: he has become a senior engineer of Huawei since he was 22. The code is what I want to say to the world
Cai Guoqiang's fireworks NFT debut is as wonderful as fireworks during the day
GBASE 8s 共享内存缓冲池介绍
[the first contact between Android engineers and smart home products ③] the specific implementation of smartconfig one key distribution network on the hardware side | the specific implementation of es
Interview eight part essay (disorderly order, no classification)
Chris LATTNER, father of llvm: the golden age of compilers
Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
ai2022新功能,illustrator 2022 新功能介绍
On regular expression matching cryptography
What is the legal basis and procedure for Tami dog sharing | state owned equity transfer?
Garlic Junkai company (DFS full arrangement)
New functions of ai2022, introduction to new functions of illustrator 2022


