C#LeetCode刷题之#771-宝石与石头(Jewels and Stones)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- LeetCode 771: 宝石与石头 Jewels and Stones
题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. You're given strings ...
- leetcode-解题记录 771. 宝石与石头
题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符 ...
- [Leetcode 771]宝石和石子 Jewels and Stones HashSet简单应用
[题目] You're given strings J representing the types of stones that are jewels, and S representing the ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- Java开发中的eclispe常用快捷键&全部快捷键
Java开发中的eclispe常用快捷键&全部快捷键 Ctrl+1 快速修复(经典快捷键)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ ...
- 用Vue实现一个简单的图片轮播
本文已收录至https://github.com/likekk/studyBlog欢迎大家star,共同学习,共同进步.如果文章有错误的地方,欢迎大家指出.后期将在将GitHub上规划前端学习的路线和 ...
- html 转义和反转义
public static void main(String[] args) {// String html = "<img style=\"width: 100%; hei ...
- Ethical Hacking - NETWORK PENETRATION TESTING(3)
Change MAC Address using macchanger. Packet Sniffing Basics Airodump-ng airodump-ng is a program pa ...
- 设计模式:template method模式
思想:在父类中定义处理流程的框架,在子类中实现具体的处理方法 优点:在父类中定义处理的算法,无需在每个子类中重复编写 继承关系图: 例子: //接口定义 class Parent { public: ...
- C++语法小记---类型转换
类型转换 C++类型转换主要包含两种: 隐式类型转换 小类型向大类型转换 char -> int short ->int int -> unsigned int int -> ...
- SparkCore
一.概述 1,定义 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象.代码中是一个抽象类,它代表一个不可变.可分区.里面的元素可 ...
- Win7安装Python失败 提示Setup failed
一.安装报错 如图所示,双击Python安装包后进行安装显示Setup failed 安装失败: 二.错误排除 1.首先查看自己的计算机是否已经安装了 Win7 Service Pack 1大补丁,没 ...
- python-study-文件操作
# 一.文件操作的作用 :读取内容.写入内容.备份内容.... # 文件的基本操作,文件操作包含:打开.关闭.读.写.复制.... # 打开 读写 关闭 # 文件备份 # 文件和文件夹的操作 # 总结 ...
- Python 编程语言的核心是什么?
01 Python 编程语言的核心是什么? 为什么要问这个问题? 我想要用Python实现WebAssembly,这并不是什么秘密.这不仅可以让Python进入浏览器,而且由于iOS和Andr ...