[LeetCode] Jewels and Stones 珠宝和石头
You're given strings J
representing the types of stones that are jewels, and S
representing the stones you have. Each character in S
is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J
are guaranteed distinct, and all characters in J
and S
are letters. Letters are case sensitive, so "a"
is considered a different type of stone from "A"
.
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Note:
S
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
这道题给了我们两个字符串,珠宝字符串J和石头字符串S,其中J中的每个字符都是珠宝,S中的每个字符都是石头,问我们S中有多少个珠宝。这道题没什么难度,高于八成的Accept率也应证了其Easy难度实至名归。那么先来暴力搜索吧,就将S中的每个字符都在J中搜索一遍,搜索到了就break掉,参见代码如下:
解法一:
class Solution {
public:
int numJewelsInStones(string J, string S) {
int res = ;
for (char s : S) {
for (char j : J) {
if (s == j) {
++res; break;
}
}
}
return res;
}
};
我们用HashSet来优化时间复杂度,将珠宝字符串J中的所有字符都放入HashSet中,然后遍历石头字符串中的每个字符,到HashSet中查找是否存在,存在的话计数器自增1即可,参见代码如下:
解法二:
class Solution {
public:
int numJewelsInStones(string J, string S) {
int res = ;
unordered_set<char> s;
for (char c : J) s.insert(c);
for (char c : S) {
if (s.count(c)) ++res;
}
return res;
}
};
参考资料:
https://leetcode.com/problems/jewels-and-stones/solution/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Jewels and Stones 珠宝和石头的更多相关文章
- [LeetCode] 771. Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- 【LeetCode】Jewels and Stones(宝石与石头)
这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- 771. Jewels and Stones珠宝数组和石头数组中的字母对应
[抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...
- Leetcode771.Jewels and Stones宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
随机推荐
- springBoot总结
springBoot总结: ssm基本的依赖: <dependencies> <!--添加依赖thymeleaf 可以访问html页面--> <!--<depend ...
- SpringBoot系列: 单元测试2
之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...
- 9、el表达式的使用
一.EL表达式的作用: 1).使用变量访问web域中存储的对象 ${user } 2).访问javabean的属性 ${user.address.city } 3).执行基本的逻辑运算(el表达式 ...
- pycharm实用快捷键集锦
以下是本人需要记录的快捷键,并不针对大众,所以是断断续续补充的,大家看看图个乐呵就成! 生成代码块(Surround with):Ctrl + Alt + t . 历史浏览页面跳转:很多时候,我们需要 ...
- TCP-IP详解笔记7
TCP-IP详解笔记7 TCP: 传输控制协议(初步) 使用差错校正码来纠正通信问题, 自动重复请求(Automatic Repeat Request, ARQ). 分组重新排序, 分组复制, 分组丢 ...
- mysql慢日志, 锁表情况查询
2018-5-29 9:10:15 星期二 锁表情况查询: show OPEN TABLES where In_use > ; 慢日志记录: 1. 修改配置文件, 重启服务后永久生效 slow_ ...
- 使用virtualenvwrapper模块管理python虚拟环境
1.pip安装virtualenvwrapper [root@localhost ~]# pip install virtualenvwrapper # 安装virtualenvwrapper [ro ...
- 金蝶K/3 BOS产品培训教案
K/3 BOS产品培训教案 1 K/3 BOS IDE练习案例... 2 1.1新建基础资料... 2 1.1.1新增基础资料交货地点... 2 1.2新建业务单据... 2 1.2.1新建寄 ...
- spring boot vuejs
项目介绍 Spring Boot 2.0 + VueJs 2.0 开发项目示例 GitHub: https://github.com/fqybzhangji/spring-boot-vue 码云: h ...
- 利用fastjson解析json并通过js&ajax实现页面的无跳转刷新
1.json是一种优秀的数据格式,在移动开发和web开发中经常用到,本例中通过一个小案例讲解如何通过alibaba的开源框架fastjson来解析jason数据格式并通过js实现无跳转刷新 2,新建一 ...