问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. Ethical Hacking - NETWORK PENETRATION TESTING(6)

    Creating a fake access point (honeypot) Fake access points can be handy in many scenarios, one examp ...

  2. UC 网盘:我又回来了

    普通用户不限速下载,免费 10GB 空间,支持离线下载 这个域名非常厉害,某里挑选域名果然是值得称赞的.直接使用手机号即可注册登录,默认赠送 10GB 空间.不过目前好像没看到有电脑客户端,电脑上下载 ...

  3. PyQt5主界面

    QMainWindow QMainWindow控件继承之QWidget控件,QWidget是所有控件的父类,主要提供界面的基本功能,包括边框.标题.工具栏.关闭按钮.最小化按钮以及最大化按钮等.子类中 ...

  4. 原来python还可以这样处理文件

    首先我为大家介绍一下python语言吧! Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言 ...

  5. IDEA破解2018年12月

    ---恢复内容开始--- 首先是这个强大的贡献者: http://idea.lanyus.com/ step1.下载IDEA下载包 https://www.jetbrains.com/idea/dow ...

  6. ~~网络编程(八):UDP~~

    进击のpython ***** 网络编程--UDP 那现在看到这里的 这就是网络编程的最后一讲了 上面讲的都是关于TCP的编程方法 还记得TCP和UDP传输的区别吗? UDP简单就简单到它可以不借助管 ...

  7. Centos 7下编译安装PHP7.2(与Nginx搭配的安装方式)

    一.下载源码包 百度云网盘下载地址:https://pan.baidu.com/s/1li4oD3qjvFyIaEZQt2NVRg 提取码:4yde 二.安装php依赖组件 yum -y instal ...

  8. centos7安装部署docker

    Kubernetes/K8s架构师实战集训营[中级班]:https://pan.baidu.com/s/1FWAz2V7BPsObixlZyW93sw 提取码:mvu0 Kubernetes/K8s架 ...

  9. jupyter的服务器配置安装

    该教程主要针对的是服务器安装,且在后台保持稳定运行的情况. 1.jupyter下载 有网的时候 1. pip install jupyter 离线安装 在有网络的环境下载安装包 2. pip down ...

  10. Spring学习之Spring与Mybatis的两种整合方式

    本机使用IDEA 2020.1.MySql 8.0.19,通过Maven进行构建 环境准备 导入maven依赖包 <dependencies> <dependency> < ...