题目描述

给定字符串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(宝石与石头)的更多相关文章

  1. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  2. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  3. [LeetCode] 771. Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  4. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  5. LeetCode 771 Jewels and Stones 解题报告

    题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...

  6. Leetcode771.Jewels and Stones宝石与石头

    给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...

  7. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  8. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

  9. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

随机推荐

  1. bouncing-balls-evil-circle

    效果如下 代码目录 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...

  2. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  3. CSS 条件判断、等宽字体以及ch单位

    <!DOCTYPE> <html lang="en"> <head> <meta charset="utf-8"> ...

  4. TODO java 作业-梭哈--待完成

    作业:定义一个类,该类用于封装一桌梭哈游戏,这个类应该包含桌上剩下的牌的信息,并包含5个玩家的状态的信息,他们各自的位置,游戏状态(正在游戏或已放弃),手上已有的牌等信息.如果有可能,这个类还应该实现 ...

  5. E212: Can't open file for writing

    意思是不能保存. 原因是权限不够,普通用户用vi 进行不了保存,需要使用超级用户才可以 命令:sudo su     转换成超级用户 vi hello       打开文件 :wq 即可保存退出

  6. mysql 自定义函数与自定义存储过程的调用方法

    存储过程:call  过程名(参数) 函数:      select  函数名(参数)

  7. Expected value at 1:0 异常解决方法

    有时候自己也很郁闷,明明自己写的是ok的竟然,还报错. 网上查找了这个异常,竟然没有解决方法,后来尝试着去解决,竟然真的解决了. 其实,我又新建一个文件夹,把原先的代码给粘贴复制进去就ok了,其实到现 ...

  8. CSS样式链接和文字常用属性

    行内: <div style="color:red;"></div> 内嵌<style>div{background-color:red;}&l ...

  9. java类的加载和执行顺序

    以前面试总会碰到涉及到类的执行过程的笔试题.下面记录我自己的测试结果: public class ClassA { public static ClassA classa = new ClassA() ...

  10. NGUI-实例化问题

    大家好,我是蜀云泉,我的博客存在的不足之处,希望大家包涵. 我在研究NGUI的时候发现一个问题.在NGUI下实例化物体,其坐标总是位于原点,也就是UIRoot的(0,0,0)处,困惑了好几天我才发现N ...