Leetcode#771.Jewels and Stones(宝石与石头)
题目描述
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
示例 1:
输入: J = "aA", S = "aAAbbbb"
输出: 3
示例 2:
输入: J = "z", S = "ZZ"
输出: 0
思路
两两比较J和S中的字符,若相同则加1。
代码实现
package Array;
import java.util.HashSet;
import java.util.Set;
/**
* 771.Jewels and Stones(宝石与石头)
* 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
* J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
*/
public class Solution771 {
public static void main(String[] args) {
Solution771 solution771 = new Solution771();
String J = "z";
String S = "ZZ";
System.out.println(solution771.numJewelsInStones(J, S));
}
public int numJewelsInStones(String J, String S) {
int num = 0;
int lenJ = J.length();
int lenS = S.length();
for (int i = 0; i < lenJ; i++) {
for (int j = 0; j < lenS; j++) {
if (S.charAt(j) == J.charAt(i)) {
num++;
}
}
}
return num;
}
public int numJewelsInStones_2(String J, String S) {
int num = 0;
Set set = new HashSet();
for (char c : J.toCharArray()) {
set.add(c);
}
for (char s : S.toCharArray()) {
if (set.contains(s)) {
num++;
}
}
return num;
}
}
Leetcode#771.Jewels and Stones(宝石与石头)的更多相关文章
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- 【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
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 ...
随机推荐
- Win32-API: 终于能正常的捕获焦点事件: WM_COMMAND、BN_SETFOCUS、EN_SETFOCUS
学习和使用win32-api来写窗口程序算算也有二三年了. 以前有个需求,想捕获控件的焦点事件(SETFOCUS. KILLFOCUS),我一直认为应该捕获 WM_SETFOCUS 和 WM_KILL ...
- win10开机自启动
快捷方式复制到win+ R 编辑shell:startup enter后的文件夹中
- IO处理要注意的事:关闭资源!关闭资源!关闭资源!
案例1: 现象: 同事做本地txt数据切分然后处理,发现删除本地临时文件目录中的文件时,有时成功,有时删除完了发现文件还在.代码各处都不报错,且各种日志打印正常. 解决: 最后发现,是业务逻辑代码中有 ...
- 3 字节的 UTF-8 序列的字节 2 无效
由于目前写完了[消息队列]模块,想做个单元测试,所以就利用spring的import标签,将mq的配置文件加入了配置.结果出现了<3 字节的 UTF-8 序列的字节 2 无效>这个问题. ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- c#线程2
多线程中很有可能存在争夺一个变量资源而产生死锁或者不被期望的结果. 测试类; class TestClass { ; private object objLock = new object(); pu ...
- POJ 3249 Test for Job (记忆化搜索)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11830 Accepted: 2814 Des ...
- 温故知新 —— Floyd算法
什么?Floyd?sb O(n ^ 3) 算法早不用了,右上角红叉吧.我之前虽然也认识过 Floyd 算法的重要性,不过多少也是这么想的.然而最近三天连续 rand 到了好几道有关的题目,让我彻底重新 ...
- linux下的crontab安装及简单使用
1.安装 # yum install vixie-cron # yum install crontabs # chkconfig crond on #设为开机启动,先要安装chkconfig(yum ...
- 2.Diango学习
##创建应用 python manage.py startapp blog !!创建应用后要添加应用,名称不允许与模块名称相同 ##应用目录结构 ##文件介绍 1. 2. 3. 4. 5. 6. ## ...