题目如下

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. Postman安装及入门实践(以百度搜索为例)

    一.Postman安装 可以FQ的小伙伴可以直接去官网下载:https://www.getpostman.com 如果不能,可以用我的安装包,版本找最新的:链接:https://pan.baidu.c ...

  2. 【Python】回文palindrome——利用字符串反转

    回文 palindrome Python 字符串反转string[::-1] Slice notation "[a : b : c]" means "count in i ...

  3. svn up (svn update) 状态缩写含义

    A:add,新增  C:conflict,冲突  D:delete,删除  M:modify,本地已经修改  G:modify and merGed,本地文件修改并且和服务器的进行合并  U:upda ...

  4. Linux Firefox Adobe Flash Player 安装和更新

    1.下载 Firefox Adobe Flash Player 使用Linux上的火狐浏览器访问如下的下载网址: https://get.adobe.com/flashplayer/ 选择下载 &qu ...

  5. Linux ->> Apt-get命令安装软件

    Apt全称Advanced Package Tool.Apt-get适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. 用法: apt-cache ...

  6. mongodb 3.4复制搭建

    mongodb数据库主从复制的原理:在主从结构中,主节点的操作记录称为oplog(operation log).oplog存储在一个系统数据库local的集合oplog.$main中,这个集合的每个文 ...

  7. MySQL案例02:ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

    MySQL在授权用户时出现报错信息,具体信息如下: 一.错误信息 执行命令: GRANT SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SH ...

  8. urlx

    2015-09-24 23:41:26 centos6.6下安装MongoDB3.0.1 https://www.percona.com/doc/percona-tokumx/installation ...

  9. 使用Android Studio和Gradle编译NDK项目之Experimental Plugin User Guide

    转载自:http://tools.android.com/tech-docs/new-build-system/gradle-experimental Introduction The new exp ...

  10. 针对ArcGIS Server 跨域问题的解释

    在博客Hello World文章中提起过,以前在sinaapp中建立过wordpress博客,也写过一篇关于ArcGIS JavaScript API的文章,但是由于sinaapp开始收费以后,我的个 ...