You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis 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 and J 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/

https://leetcode.com/problems/jewels-and-stones/discuss/113553/C++JavaPython-Easy-and-Concise-Solution-O(M+N)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Jewels and Stones 珠宝和石头的更多相关文章

  1. [LeetCode] 771. Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  2. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  3. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  4. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  5. 771. Jewels and Stones珠宝数组和石头数组中的字母对应

    [抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...

  6. Leetcode771.Jewels and Stones宝石与石头

    给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...

  7. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  8. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  9. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

随机推荐

  1. 第二节: 比较EF的Lambda查询和Linq查询写法的区别

    简介 在前面EF的介绍中,曾多次提到过EF可以使用Lambda和Linq来完成对数据库的访问,这两种的语法的具体使用和注意事项在前面的DotNet进阶的系列章节中已经详细介绍过了,本次借着EF章节,重 ...

  2. MyBatis # $区别

    方式一: <select id="getUserById" resultType="User" parameterType=”int”> SELEC ...

  3. css 兼容各种iPhone

    @media (device-height:480px) and (-webkit-min-device-pixel-ratio:2){/* 兼容iphone4/4s */ .class{} } @m ...

  4. mvc 在弹出框中实现文件下载

    var myParent = parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent; ...

  5. Java反射定义、获取Class三种方法

    反射机制的定义: 在运行状态时(动态的),对于任意一个类,都能够得到这个类的所有属性和方法.  对于任意一个对象,都能够调用它的任意属性和方法. Class类是反射机制的起源,我们得到Class类对象 ...

  6. 410 for 循环 运算 改变循环的控制流 死循环 遍历数组 定义方法 有名函数匿名函数 定义函数的方法取值 date math 局部变量 函数 局部与全局变量 次幂/随机数/取绝对值/向上取整/平方根

    for(1.表达式1;2.表达式2;3.表达式3){ 4.循环体语句; } 先执行1 ,在执行2, 表达式, 如果2结果为false,退出循环 如果2是true 执行4 在执行3 执行2 举例打印1- ...

  7. decltype类型指示符

    C++11新标准引入第二种类型说明符decltype,它的作用是选择并返回操作数的数据类型. 编译器分析表达式并得到它的类型,却不实际计算表达式的值: decltype(f()) sum = x;// ...

  8. Hough transform(霍夫变换)

    主要内容: 1.Hough变换的算法思想 2.直线检测 3.圆.椭圆检测 4.程序实现 一.Hough变换简介 Hough变换是图像处理中从图像中识别几何形状的基本方法之一.Hough变换的基本原理在 ...

  9. thymeleaf时间格式化

    Thymeleaf模板时间格式表达式     ${#dates.format(date, 'dd/MMM/yyyy HH:mm')} 例如: <input name="enroDate ...

  10. Excel 2010如何打开多个独立窗口?

    https://jingyan.baidu.com/article/86fae346acca7d3c49121ad4.html