Level:

  Easy

题目描述:

You're given strings J representing the types of stones that are jewels, and Srepresenting 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 and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

思路分析:

  将珠宝包含的字符放进map中,然后遍历石头,如果遍历到的字符出现在map中,那么石头中的珠宝数目就加一。

代码:

class Solution {
public int numJewelsInStones(String J, String S) {
HashMap<Integer,Character>map=new HashMap<>();
int res=0;
if(J==null||S==null)
return res;
for(int i=0;i<J.length();i++){
map.put(i,J.charAt(i));
}
for(int j=0;j<S.length();j++){
if(map.containsValue(S.charAt(j)))
res++;
}
return res;
}
}

18.Jewels and Stones(珠宝和石头)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. 1038. Jewels And Stones

    描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因此& ...

随机推荐

  1. Python类(三)-多继承的区别

    多继承的有两个方式,一个是广度优先,一个是深度优先Python2中经典类按深度优先,新式类按广度优先Python3中经典类和新式类都按广度优先 # -*- coding:utf-8 -*- __aut ...

  2. Changing Controller Numbers in Solaris

    If you need to change the controller numbers (c#) that a disk has assigned to it, whether it is for ...

  3. DAY17-Django之model增删改

    添加表记录 普通字段 #方式1 publish_obj=Publish(name="人民出版社",city="北京",email="renMin@16 ...

  4. 第一天:tomcat相关知识和浏览器的访问机制

    1.tomcat的目录结构 1)bin目录:启动和关闭tomcat以及其他的脚本命令  2)conf目录:存放各种配置文件 a.server.xml配置文件的配置: *<host/>标签: ...

  5. HTML布局,插件的调用方法

  6. 浅析大数据的技术生态圈(Hadoop,hive,spark)

    大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞生的.你可以把它比作一个厨房所以需要的各种工具.锅碗瓢盆,各有各的用处,互相之间又有重合.你可 ...

  7. cd命令无效

    原因是没有切换盘符步骤一:C:\Documents and Settings\Administrator>d:步骤二:cd D:\Program Files\Python35-32\Script ...

  8. Java3D读取3DMax模型并实现鼠标拖拽、旋转、滚轮缩放等功能

    /**-------------------------------------------------代码区--------------------------------------------- ...

  9. 机器人自主移动的秘密:SLAM与路径规划有什么关系?(三)

    博客转载自:https://www.leiphone.com/news/201612/lvDXqY82OGNqEiyl.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...

  10. cJSON

    http://blog.csdn.net/wangchangshuai0010/article/details/18225423