18.Jewels and Stones(珠宝和石头)
Level:
Easy
题目描述:
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.
思路分析:
将珠宝包含的字符放进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(珠宝和石头)的更多相关文章
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [LeetCode] 771. Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- 771. Jewels and Stones珠宝数组和石头数组中的字母对应
[抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...
- 【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 ...
- Leetcode771.Jewels and Stones宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- 1038. Jewels And Stones
描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因此& ...
随机推荐
- jhipster初接触
在Windows7部署之前把几个依赖下了 jdk:1.80 Maven :3.3.9 git:2.14.1 npm:唯一要注意的就是配置一个阿里的镜像,不然慢的你崩溃 Yeoman: npm inst ...
- leetcode429
这道题目是属于树的层次遍历,使用两层的队列非空判断. class Solution { public: vector<vector<int>> levelOrder(Node* ...
- DAY11-MYSQL单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...
- ListView显示Sqlite的数据美化版与性能优化
在上一篇文章中,我们已经实现在listview显示数据库内容的.但是我们listview中,排版不是很好看,所以这篇文章呢,我们来对listveiw进行美化.哈哈,说白了,就是对listview添加一 ...
- java判断姓是否合格 百家姓
package util; import java.lang.reflect.Array; public class FirstName { public static boolean ClearNa ...
- Android远程服务(AIDL)实现步骤
AIDL是安卓接口定义语言的缩写 由于笔者使用的是android studio所以建立AIDL文件的位置也需要注意,要在APPNAME->main->aidl->packagenam ...
- ZF 语法
Zend Framework Command Line Console Tool v1.11.11 Details for action "" and provider " ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- TypeError: document.body is null_js报错解决办法
今天在使用如下js代码的时候,发现报错:document.body is null <script type="text/javascript"> var dw=doc ...
- 数据结构_wow(泡泡的饭碗)
问题描述 饱了吗终于发现泡泡破解了它的代码并借此白吃白喝.饱了吗当即改变了自己的幸运儿生成源码,但是,又被机智的泡泡偷瞄到了,机智的泡泡马上意识到可能要饭碗不保了:每当有人参与抽奖,这个人就进入队列. ...