LeetCode 771 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".
Input: J = "aA", S = "aAAbbbb"
Output: 3
S and J will consist of letters and have length at most 50.The characters in J are distinct.
题目分析及思路
题目要求J中的字符都不一样,找出S中与J中字符相同的字符个数。可以用两层循环遍历两个字符串,还可引入集合进行比较。
python代码
class Solution:
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
jewels = set(J)
sum = 0
for c in S:
if c in jewels:
sum += 1
return sum
LeetCode 771 Jewels and Stones 解题报告的更多相关文章
- 【LeetCode】771. Jewels and Stones 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- 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 (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- [LeetCode] 771. Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- json-server 详解
JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. 安装json-server npm install -g json ...
- android开发(47) 使用xml drawable 实现 局部圆角,可用作圆角边框
代码如下: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=" ...
- c# 使用GDAL处理大图
注意问题: 1.GDAL 使用官网生成好的dll,必须把Bin目录下的dll一并加到执行目录下去,否则会出错. 2. 用环境变量设置引用路径可以避免一大堆dll放一起.代码如下: /// <s ...
- Java知多少(77)日期和时间类
Java 的日期和时间类位于 java.util 包中.利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间. Date 类 Date 类是 Java 中的日期时间类 ...
- DWZ使用中遇到的坑
DWZ官方文档中关于文件上传表单的提交: 因为Ajax不支持enctype="multipart/form-data" 所以用隐藏iframe来处理无刷新表单提交. <for ...
- IOS开发之--iPhone XR,iPhone XS Max适配
因为iPhone X和iPhone XS的尺寸比是一样的,只需要把这两张图片补上就行. 具体原理性的东西就多说了,因为iPhoneX系列都一样,本文只说明一下具体怎么做,要适配屏幕,首先得让他以正确的 ...
- vim.sh
#!/bin/bash #https://github.com/txthinking mkdir /tmp/_ curl https://raw.githubusercontent.com/txthi ...
- 网页使用思源字体 CSS
在知乎上再次看到这门字体的提问,想想中文字体过得多么艰辛,中文软件过得多么艰辛. 思源字体 2014年7月,Adobe与Google宣布推出一款新的开源字体思源黑体, 有七种字体粗细(ExtraLig ...
- day_6.22python多线程
僵尸进程:子进程结束,父类未结束 孤儿进程:父类进程over.,子进程未结束 0号进程负责运行,1号进程负责生成,所有孤儿进程的收容所(孤儿进程:父类进程over)1号进程,永不结束! Linux: ...
- E - TOYS
来源 poj 2318 Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad ...