题目如下

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的更多相关文章

  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 ...

  10. 1038. Jewels And Stones

    描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因此& ...

随机推荐

  1. 《火星救援》NASA惊现lisp

    duang-跳出个界面上面一个lisp程序.

  2. 简单的自动升级提示AutoUpdater

    看过网上“圣殿骑士”和其他人的升级做法,感觉不太适合几十M的小型软件. 之前用的一直都是clickonce,但是3年下来感觉弊端太多,比如安装不能选择文件夹.打包不全.版本等问题,于是决定另辟捷径. ...

  3. QT 定时执行某个函数,隐藏某个控件

    QTimer::singleShot(3000, this, SLOT(slotHideFinishedLabel())); // 这里是一个3秒定时器, 且只执行一次. #include " ...

  4. 【PHP系列】框架的抉择

    缘起 在PHP开发中,选择合适的框架有助于加快软件开发,节约宝贵的项目时间,让开发者专注于功能的实现上.框架的问题是需要很多的投入,选择框架时,我们更看重这个框架的未来,存在多年的大型框架必须要有好的 ...

  5. 使用github参与到开源项目的维护

    参与到开源项目的维护工作一般分两种,一种是由项目建立者拉入到贡献者列表中,拥有对项目的读写权限,而普通用户对项目仅有读取权限,另一种是fork项目到自己仓库,然后把修改后的内容发送给项目管理者者请求合 ...

  6. 【pbrt】在c++程序中使用pbrt进行渲染

    近段时间做一个关于水面的动画.由于我用c++实现水面动画的,然而使用c++我自己的渲染系统渲染结果被同学说是可视化不叫渲染,所以我决定修改一下…… 恰好进来在学习pbrt,所以索性就蛋疼了考虑直接用p ...

  7. 你是怎么调试 JavaScript 程序

    你是怎么调试 JavaScript 程序的?最原始的方法是用 alert() 在页面上打印内容,稍微改进一点的方法是用 console.log() 在 JavaScript 控制台上输出内容.嗯~,用 ...

  8. tp5中分页携带参数的方法

    $list = $model->where(...)->order(.....)->paginate($size, false, [                'query' = ...

  9. sublime text html5开发学习 插件篇记录

    1.第一步先按照 Package Control,具体步骤自行百度,Google. 2. view in browser 默认的快捷键应该是这样的,我用的是IE浏览器.所以ctrl+alt+i 即可让 ...

  10. openlayers中的自定制工具栏,包含画点、线、面

    先是在projectquantan-master这个项目中有一个EditingPanel这个工具条,也挺好的,功能挺全的,但是有一点就是只有画多边形的一个按钮,没有point和path俩个的,所以就想 ...