当前位置:网站首页>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
边栏推荐
- Introduction to granularity locking of gbase 8s concurrency control
- Optical cat super account password, reset optical cat to obtain super account password
- Gbase 8s group by function introduction
- Gbase 8t index
- Gbase 8s数据库日志简介及管理
- Gbase 8s Group by 功能介绍
- Prince saves Princess (DFS)
- Is the stable currency a super opportunity to accelerate the death of the public chain or replace BTC?
- Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
- Interview eight part essay (disorderly order, no classification)
猜你喜欢

Small example of gin - get request 2-handle handles post requests

Basic operation of Android local database | multi thread operation database | addition, deletion, modification and query of database | batch insertion into database | basic use of thread pool | Yu nia

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

Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!

Counting garlic customers: Sudoku (DFS)

engine.POST()处理POST请求

全排列(DFS和next_permutation解法)

引爆炸弹(DFS)

Slow response of analysis API

京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。
随机推荐
Gbase 8s客户端与服务器的通信
Function encapsulation such as addition, deletion, modification and query of linked list (summary)
Generating class diagram with EA reverse engineering code
Use Charles to grab app package
Gbase 8s 共享内存段删除
天梯赛L2-6 树的遍历
Examples of branch and loop statements
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
CVPR | 2022 | expressed by transformer learning multiple hypotheses! A new framework for 3D human pose estimation!
Slow response of analysis API
Configuration of imx6ull bare metal development analysis and configuration process of elcdif lighting RGB LCD
Full Permutation (DFS and next_permutation solution)
iTextSharp 显示中文字体
Counting garlic guest: the solution of the equation
Three technical solutions of ant group were selected as "typical solutions for information technology application and innovation in 2021"
gin--hello
2n皇后问题
Blocking type of gbase 8s concurrency control
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
Basic operation of Android local database | multi thread operation database | addition, deletion, modification and query of database | batch insertion into database | basic use of thread pool | Yu nia


