1038. Jewels And Stones
描述
给定字符串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的更多相关文章
- 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(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- 771. Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
随机推荐
- jq插件模板
!(function($,window, document, undefined){ function Plugin(ele,options){ //默认参数设置 this.defaults={ pa ...
- Java并发编程-并发工具类及线程池
JUC中提供了几个比较常用的并发工具类,比如CountDownLatch.CyclicBarrier.Semaphore. CountDownLatch: countdownlatch是一个同步工具类 ...
- CF451E
一道不错的题,对排列组合能力的要求较高 题意:给定s个相同的小球放在n个不同的盒子里,可以不放,每个盒子有一个放的上限,求一共有多少种放法 解析:首先考虑没有上限的情况,这里比较好解决,采用隔板法,可 ...
- solt插槽简单使用实例
在父组件内可以定义方法,变量 等,还可以在父组件中使用呢. 样式可以在子组件里写,也可以在父组件写. 子组件: <template> <div class="admin-u ...
- servlet设置cookie
Cookie cookie =new Cookie("user","黄花菜");//实例化一个Cookie对象 cookie.setMaxAge(7*24*60 ...
- [转] Node.js中package.json中库的版本号详解(^和~区别)
当我们查看package.json中已安装的库的时候,会发现他们的版本号之前都会加一个符号,有的是插入符号(^),有的是波浪符号(~).那么他们到底有什么区别呢?先贴一个例子,对照例子来做解释: bl ...
- 5336: [TJOI2018]party
题解: 比较水啦..dp套dp f[i][j][k]表示枚举了前i位,最大公共子序列匹配状态为j,noi匹配到了第k位 因为g[j]和g[j+1]最多差1 所以可以状压成j 然后内层再dp一下搞出下一 ...
- SqlSever大数据分页【转】
在sql sever中大数据的分页一直是难以处理的一块,利用id自增列分页也存在不足之处.从一个相对全面的分页看,sql sever2005中新增的row_number()函数解决了这个问题.还 ...
- keepalived当主节点切换时脚本通知 lvs
脚本 在keepalived.conf中添加 mail 查看邮件 实验dr实验模型 给director 做主从 4台服务器 rip:192.168.0.103 rip2:192.168.0.104 ...
- 使用Phar来打包发布PHP程序
简单来说,Phar就是把Java界的jar概念移植到了PHP界. Phar可以将一组PHP文件进行打包,还可以创建默认执行的stub(或者叫做 bootstrap loader),Phar可以选择是否 ...