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 ...
随机推荐
- 第三篇-以LinearLayout进行Android界面设计
一.新建一个项目 选择empty activity,此时项目里面没有main.java的文件. 二.手动创建java文件 Project那儿选择android模式,在app/java/com....一 ...
- 关于字符编码,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…)
字符编码的问题看似很小,经常被技术人员忽视,但是很容易导致一些莫名其妙的问题.这里总结了一下字符编码的一些普及性的知识,希望对大家有所帮助. 还是得从ASCII码说起 说到字符编码,不得不说ASCII ...
- unittest的使用一
selenium: (1).firefox官方下载驱动geckodriver,windows:放在\python36或者是27的目录下 Mac: /usr/local/bin (2).firefox的 ...
- poj 3258"River Hopscotch"(二分搜索+最大化最小值问题)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 块岩石,从中去掉任意 M 块后,求相邻两块岩石最小距离最大是多少? 题解 ...
- fork()、vfork()、clone()的区别
因为生活的复杂,这是一个并行的世界,在同一时刻,会发生很多奇妙的事情,北方下雪,南方下雨,这里在吃饭,那边在睡觉,有人在学习,有人在运动,所以这时一个多彩多姿的世界,每天都发生着很多事情,所以要想很好 ...
- php调用API支付接口 转至http://www.cnblogs.com/chaochao00o/p/6490463.html
首先访问 https://charging.teegon.com/ 注册账号, 找到开发配置 记下client_id和client_secret. 点击 天工开放平台 点击天工收银 点击 S ...
- day16-(listener&filter)
回顾: ajax: 异步请求 原生的ajax(了解) 1.创建一个核心对象 XMLHttpRequest 2.编写回调函数 xmlhttp.onreadystatechange=function(){ ...
- 1053. Path of Equal Weight (30)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...
- MyBatis-获取 xxxMapper
Main 方法,mybatis 版本为 3.5.0 使用 MapperProxyFactory 创建一个 MapperProxy 的代理对象 代理对象里面包含了 DefaultSqlSession(E ...
- twitter分布式主键id生成器
pom <!--生成id--> <dependency> <groupId>com.github.bingoohuang</groupId> <a ...