Jewels and Stones
题目如下
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in Sis 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中找出包含的J中的字符的种类数。
直接的方式方法应该是hash映射法。
算法思想
0. 设定参数cnt用于记录S中存在的宝石数目
1. 将J中的字符映射为hash数组(字母有ascii的话需设定hash数组长度256)(J中存在的字符,hash数组中对应的ascii码编号位置处设为1)
2.读取S中的字符c,如果hash[c] = 1,则cnt++;
3.返回cnt
代码实现
1. java代码
class Solution {
public int numJewelsInStones(String J, String S) {
int[] hash = new int[256];
int ans = 0;
for(int i=0; i<J.length(); i++)
{
hash[J.charAt(i)] = 1;
}
for(int i=0; i<S.length(); i++)
{
if(hash[S.charAt(i)] == 1) ans++;
}
return ans;
}
}
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 ...
- 1038. Jewels And Stones
描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因此& ...
随机推荐
- C语言--清理getchar缓存
getchar()采用了缓冲区,而getch()才是立即获取,所以要想再用getchar()获取正确的值必须先清空缓冲区,如果是windows操作系统,用fflush(stdin)函数或rewind( ...
- 【Machine Learning】训练集 验证集 测试集区别
最近在Udacity上学习Machine learning课程,对于验证集.测试集和训练集的相关概念有些模糊.故整理相关资料如下. 交叉检验(Cross Validation) 在数据分析中,有些算法 ...
- Qt QDialog将窗体变为顶层窗体(activateWindow(); 和 raise() )
m_pLoginDlg->hide(); m_pLoginDlg->activateWindow(); //m_pLoginDlg->raise(); m_pLoginDlg-> ...
- 如何编写自定义hive UDF函数
Hive可以允许用户编写自己定义的函数UDF,来在查询中使用.Hive中有3种UDF: UDF:操作单个数据行,产生单个数据行: UDAF:操作多个数据行,产生一个数据行. UDTF:操作一个数据行, ...
- Mac下终端自动补全功能
记录一下终端的一些使用命令,跟自动补全的配置,主要怕以后忘记了. 1.终端自动补全的配置 打开终端,输入 : nano .inputrc 在文件里面写上: set completion-ignore- ...
- git中忽略文件权限或文件拥有者的改变
在发布项目到线上时,很多时候需要修改文件的权限,如果是使用git版本管理软件来发布的话,那么下次更新线上文件的时候就会提示文件冲突.明明文件没有修改,为什么会冲突呢?原来git把文件权限也算作文件差异 ...
- 个体商户POS机遭遇禁刷 职业养卡人称自有对策
“套现猛于虎也”,这对于信用卡业而言无异于一大命门,信用卡套现金额的规模如同滚雪球般愈演愈烈.记者昨日采访银行业内了解到,虽然为防套现将根据规定关闭个体商户POS机刷信用卡的功能,但职业“养卡人”不以 ...
- Best Time to Buy and Sell Stock II--疑惑
https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 代码如下时能AC class Solution { publi ...
- React - React Developer Tools开发者工具的安装与使用(Chrome调试插件)
原文地址:http://www.cnplugins.com/zhuanti/how-to-use-react-tools.html 虽然我们曾经在React开发者工具的基础介绍里面有概括性的介绍过Re ...
- Prototype Pattern