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 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中找出包含的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中的字符都是字母. 字母区分大小写,因此& ...
随机推荐
- sqlserver2008数据库文件降级为sqlserver2005文件
直接分离附加是不行的. 操作步骤如下: 在sqlserver2008企业管理器中 右键xx数据库->任务->生成脚本 弹出框中勾选 为所选数据库中的所有对象编写脚本 下一步 修改如下图片 ...
- jQuery ajax调接口时跨域
解决方法提炼 一.jsonp方法 在前端ajax配置jsonp参数,在后台配置jsonp设置,具体方法自行百度 二. 参考同源策略 把前端静态页面放在tomcat内webapp下,和后台文件同目录, ...
- 03_ActiveMQ安全机制
[ActiveMQ安全机制] [ ActiveMQ的web管理界面 ] 地址 http://127.0.0.1:8161/admin ActiveMQ管理控制台使用jetty部署,所以需要修改密码, ...
- maoaolianxi
package dududu; public class maopao { public static void main(String[] args) { // TODO 自动生成的方法存根 ,,, ...
- mongodb远程连接
1.关闭防火墙 firewall-cmd --state 查看防火墙是否运行中 systemctl stop firewalld.service 关闭防火墙服务 2.使用bind_ip指定 ...
- phantomjs rendering
http://wwwy3y3.ghost.io/pageres-phantomjs-capture-sreenshot-chinese-fonts-not-render-correctly/ 在使用中 ...
- MVC5中Model设置属性注解
ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证(在服务器端和客户端验证数据的有效性) 数 ...
- vmware虚拟机添加新硬盘
1 vmware中添加硬盘 2 重启虚拟机后,fdisk -l 查看已经挂在好的硬盘,比如/dev/sdb3 mkfs.ext3 /dev/sdb4 挂在: mount /de ...
- React Router V4.0学习笔记
最近在学习React Router,但是网站的教程多半还是3.X版本之前的,所以我只能在GitHub上找到React Router的官方文档在读.后来总结了一下,包括学习经验以及V3.X与V4.X的差 ...
- oracle中PLSQL存储过程中如何使用逗号分隔的集合(逗号分隔字符串转换为一个集合)
原文: https://blogs.oracle.com/aramamoo/entry/how_to_split_comma_separated_string_and_pass_to_in_claus ...