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 ...
随机推荐
- log4j 配置,tomcat 启动或有后台操作时,控制台会显示很多 DEBUG 信息
log4j 配置,tomcat 启动或有后台操作时,控制台会显示很多 DEBUG 信息 日志信息可以以文件形式显示,也可以在控制台输出,在 log4j.properties 文件设置. 控制台有很多 ...
- python爬虫获取图片
import re import os import urllib #根据给定的网址来获取网页详细信息,得到的html就是网页的源代码 def getHtml(url): page = urllib. ...
- noi.openjudge 1.13.44
http://noi.openjudge.cn/ch0113/44/ 总时间限制: 1000ms 内存限制: 65536kB 描述 将 p 进制 n 转换为 q 进制.p 和 q 的取值范围为[2 ...
- Shell 同步时间脚本
Linux系统同步时间脚本 Linux操作系统,如果时间和网络时间差距太大的话.可能会导致程序,进程启动不了.所以linux系统时间同步显得尤为重要,本文在借鉴网上众多资料后,以centos_6.X系 ...
- 我们数学中常用的自然常数e代表什么?看完长知识了!
我们在学习期间都接触过自然常数e,也知道e ≍ 2.718,学过极限的同学应该也知道 那么大家知道e的含义是什么吗?为啥叫“自然常数”? e的含义可以用一个计算利息的例子来解释. 假如你有1块钱,银行 ...
- FastDFS + Nginx代理方式访问
FastDFS + Nginx代理方式访问 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.在storage上安装nginx 1>.下载nginx软件(http://ngi ...
- git lg 使用 转
命令: git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yello ...
- Mybatis笔记三:全局配置文件
目录 配置文件 dtd提示 properties标签(不怎么用) typeAliases 自动把下划线换成驼峰命名 配置文件 看着这个配置文件,我们将对这个配置文件进行细致的讲解 <?xml v ...
- python js(JavaScript)初识
####################总结############## 引入: 可以在body标签中放入<script type=”text/javascript”></scrip ...
- Oracle优化学习
SQL执行效率对系统使用有很大影响,本文总结平时排查问题中遇到的一些Oracle优化问题的解决方案,或者日常学习所得. 1. Oracle sql执行顺序 sql语法的分析是从右到左. 1.1 SQL ...