LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table
这一题很简单,题目给了两个string:J 和 S。 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石。
Java Solution:
Runtime beats 97.77%
完成日期:03/06/2019
关键点:HashSet
class Solution
{
public int numJewelsInStones(String J, String S)
{
int result = 0; Set<Character> jewels = new HashSet<>();
// put Jewels into HashSet
for(char c : J.toCharArray())
{
jewels.add(c);
} // iterate stones to find jewels
for(char c : S.toCharArray())
{
if(jewels.contains(c))
result++;
} return result;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 771. Jewels and Stones (宝石与石头)的更多相关文章
- 【LeetCode】Jewels and Stones(宝石与石头)
这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...
- [LeetCode] 771. 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(宝石与石头)
题目描述 给定字符串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 771 Jewels and Stones 解题报告
题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...
- Leetcode771.Jewels and Stones宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
随机推荐
- js文件中引用其他js文件
这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...
- 使用libpqxx访问PostgreSQL数据库(mingw编译libpqxx)
编译前准备 1. 安装mingw 安装mingw(不管是直接安装mingw还是其他如code::blocks附带安装的mingw),输入:gcc -v可显示如下图的版本信息,我的版本是mingw ...
- PKI中常用编码:ASN.1 DER BER Base64
迟到了两年的笔记... 在PKI的应用中,常会用到以下几个编码概念: ASN.1(Abstract Syntax Notation One, 抽象语法标记) 定义:A standard interfa ...
- MySql(五)select排序查询
举个栗子/**查询员工信息,要求工资按照从高到低进行排序(默认升序)**/SELECT * FROM employees ORDER BY salary ASC;/**方法2:**/SELECT * ...
- vi 命令学习(二)
[选中文本(可视模式)] v 可视模式 从光标位置开始按正常模式选择文本 V 可视行模式 选中光标经过的完整行 ctrl + v 可视块模式 垂直方向选中文本 [ 撤销和恢复撤销] u undo 撤销 ...
- TWaver3D特效之高光反射
前篇我们介绍了TWaver 3D的环境映射特效,下面我们接着给大家分享高光反射特效.高光反射定义了物体上的某一区域比其他地方更反光.在高光反射的贴图中,黑色区域的反射率为0(完全不反光),白色区域的反 ...
- 基于springmvc、ajax,后台连接数据库的增删改查
前言 前段时间在博客园上找了一个springmvc的例子,照着学了一下,算是对springmvc有了一个初步的了解,打一个基础,下面是链接.(我只看了博客,视频太耗时间了) 博客链接:http://w ...
- 「 hihoCoder 1014 」Trie 树
标题真直接 题目大意 给你 $n$ 个字符串.存到一个字典中.又给你 $m$ 个询问,每个询问给一个字符串,在字典中查出有多少个字符串是以这个字符串为前缀. 解题思路 模板题啊 在每个点设置一个变量 ...
- [Algorithm] 5. Kth Largest Element
Description Find K-th largest element in an array. Example In array [9,3,2,4,8], the 3rd largest ele ...
- Python学习-while循环语句
Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.即重复性的做一件事情 语法形式如下: while 判断条件: 条件满足执行语句…… ...