描述

给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝.

J中的字母一定不同,J和S中的字符都是字母。 字母区分大小写,因此"a"和"A"是不同的类型.

  • S和J由字母组成且长度最大为50.
  • J中字符各不相同.

    您在真实的面试中是否遇到过这个题?

样例

样例 1:

输入: J = "aA", S = "aAAbbbb"
输出: 3

样例 2:

输入: J = "z", S = "ZZ"
输出: 0
Description
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A". S and J will consist of letters and have length at most 50.
The characters in J are distinct.
public class Solution {
/**
* @param J: the types of stones that are jewels
* @param S: representing the stones you have
* @return: how many of the stones you have are also jewels
*/
public int numJewelsInStones(String J, String S) {
// Write your code here
char[] jewelry = J.toCharArray();
char[] stone = S.toCharArray(); int res = 0; for(int i=0; i<jewelry.length; i++){ for(int j=0; j<stone.length; j++){
if (jewelry[i] == stone[j]) {
res++; } }
}
return res;
}
}

1038. Jewels And Stones的更多相关文章

  1. LeetCode --> 771. Jewels and Stones

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

  2. Leetcode#771.Jewels and Stones(宝石与石头)

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

  3. 【Leetcode】Jewels and Stones

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

  4. 【Leetcode_easy】771. Jewels and Stones

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

  5. 771. Jewels and Stones - LeetCode

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

  6. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  7. 771. Jewels and Stones

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

  8. [Swift]LeetCode771. 宝石与石头 | Jewels and Stones

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

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

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

随机推荐

  1. Mycat实现mysql主从复制(读写分离)

    数据库性能瓶颈主要原因: 随着用户数的增多,带来的是数据库连接的大幅度增长 随着业务体量的增长,表数据量(空间存储的问题)的大幅增长,其中涉及到索引的优化,mysql默认的索引是硬盘级别的,BTREE ...

  2. Windows 系统共享文件扫描

    近年来历次泄露的安全事故(工控安全),其主要原因就是内部网络自身的脆弱性问题.对于内部网络的安全检查是很必要的.传统上使用CMD命令  net view 就可以扫描在线的主机但是,主机设置取消QOS的 ...

  3. cf自训6

    cf946D 背包+区间dp 好题 /* 先预处理出每行消去i个1后可以的到的最小时间: 先求每行的前缀和,枚举左端点和右端点,消去的1 cost=tot-sum[r]+sum[l-1],区间长度=r ...

  4. cf1051d 简单的状态压缩dp

    /* 给定一个二行n列的格子,在里面填黑白色,要求通过黑白色将格子分为k块 请问有多少种填色方式 dp[j][k][0,1,2,3] 填到第j列,有k块,第j列的颜色, */ #include< ...

  5. bzoj 2186

    非常有趣的题 题意:求1~N!中有多少个与M!互质的数,T组询问,答案对R取模 题解: 首先,因为N>M,所以N!>M!,所以答案一定有一部分是φ(M!) 接下来做一些分析: 引理: 若x ...

  6. mybatis初始化过程

    mybatis初始化如下: //加载配置文件InputStream resourceAsStream = Resources.getResourceAsStream("testMybatis ...

  7. SpringMvc框架MockMvc单元测试注解及其原理分析

    来源:https://www.yoodb.com/ 首先简单介绍一下Spring,它是一个轻量级开源框架,简单的来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开 ...

  8. jQuery核心方法

    1.$(document.body).css( "background", "black" ); 2.$(myForm.elements).hide():隐藏表 ...

  9. 饮冰三年-人工智能-Python-10之C#与Python的对比

    1:注释 C# 中 单行注释:// 多行注释:/**/ python 中 单行注释:# 多行注释:“““内容””” 2:字符串 C#中 "" 用双引号如("我是字符串&q ...

  10. 集腋成裘-05-angularJS -初识angular

    私以为angular的最大特点是:只关注数据 1.1 angular之:双向绑定 <!DOCTYPE html> <html ng-app=""> < ...