问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3812 访问。

给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。

输入: J = "aA", S = "aAAbbbb"

输出: 3

输入: J = "z", S = "ZZ"

输出: 0

注意:

S 和 J 最多含有50个字母。

J 中的字符不重复。


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

Input: J = "aA", S = "aAAbbbb"

Output: 3

Input: J = "z", S = "ZZ"

Output: 0

Note:

S and J will consist of letters and have length at most 50.

The characters in J are distinct.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3812 访问。

public class Program {

    public static void Main(string[] args) {
var J = "aA";
var S = "aAAbbbb"; var res = NumJewelsInStones(J, S);
Console.WriteLine(res); J = "z";
S = "ZZ"; res = NumJewelsInStones2(J, S);
Console.WriteLine(res); Console.ReadKey();
} private static int NumJewelsInStones(string J, string S) {
var res = 0;
foreach(var c in S) {
if(J.Contains(c)) res++;
}
return res;
} private static int NumJewelsInStones2(string J, string S) {
var res = 0;
var set = new HashSet<char>();
foreach(var c in J) {
set.Add(c);
}
foreach(var c in S) {
if(set.Contains(c)) res++;
}
return res;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3812 访问。

3
0

分析:

设石头的类型数量为 m,拥有的石头的数量为 n ,那么NumJewelsInStones 的时间复杂应当为:  ,NumJewelsInStones2的时间复杂为:  。

C#LeetCode刷题之#771-宝石与石头(Jewels and Stones)的更多相关文章

  1. LeetCode 771: 宝石与石头 Jewels and Stones

    题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. You're given strings ...

  2. leetcode-解题记录 771. 宝石与石头

    题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符 ...

  3. [Leetcode 771]宝石和石子 Jewels and Stones HashSet简单应用

    [题目] You're given strings J representing the types of stones that are jewels, and S representing the ...

  4. [Swift]LeetCode771. 宝石与石头 | Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  5. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  6. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  7. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  8. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  9. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

随机推荐

  1. 产品升级前后MD5码对比

    在做产品测试的时候,经常会需要对比升级前后的MD5码,这时可以通过终端登录设备,具体步骤如下: 1.在升级前时,将MD5码写入log1.info文件: check_md5 -d / -w log1.i ...

  2. vue 应用 :多语言显示

    <template> <div class="hello2"> <page-content> </page-content> < ...

  3. .Net、ASP.Net、C#、VisualStudio之间的关系是什么

    .Net一般指的是.NetFramework,提供了基础的.Net类,这些类可以被任何一种.Net编程语言调 用,.NetFramework还提供了 CLR.JIT.GC等基础功能. ASP.Net是 ...

  4. Webpack前世今生

    在正式介绍Webpack之前,先给大家说明一下前端为什么需要模块化 1.为什么需要模块化 1.1JS原始功能 在网页开发的早期,js制作作为一种脚本语言,做一些简单的表单验证或动画实现等,那个时候代码 ...

  5. node的async模块

    废话不多说,直接开始 这个模块有几种方法.分别用于的不通的情况自己喜欢怎么用就怎么用 第一个方法,series  这个方法用于串行切无关联.什么意思那就是,里面的方法是一个一个执行的,每一个方法相互不 ...

  6. Spring bean初始化

  7. 更改docker默认存储路径操作(centos6版本)

    一. centos6版本  service启动方式 1.更改启动文件 vim /etc/sysconfig/docker   添加更改的路径 '--graph="/data/docker&q ...

  8. vue学习(十二) 指令v-if v-show 控制页面标签的显示与隐藏

    //html <div id="app"> <input type="button" value="toggle" @cl ...

  9. Python 写入excel时的字体格式设置

    转自:https://blog.csdn.net/kuangzhi9124/article/details/81940919 下面代码设置了单元格的字体.位置居中.框线,可以将格式调成自己需要的 im ...

  10. 初学Vue.js,用 vue ui 创建项目会不会被鄙视

    全栈的自我修养: 6使用vue ui进行vue.js环境搭建 It is only with the heart that one can see rightly. What is essential ...