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 ...
随机推荐
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- 15款css3鼠标悬停图片动画过渡特效
分享15款css3鼠标悬停图片动画过渡特效.这是一款15款不同效果的css3 hover动画过渡效果代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class ...
- haproxy配置详解
先看一个ha的配置文件: # # Global settings # global # to have these messages end up in /var/log/haproxy.log yo ...
- js调用winform程序(带参数)
我们会发现,我们点击迅雷下载的时候 网页可以调用应用程序,而且连接会传入迅雷,这个是怎么做到的呢? 原理: 先注册表中添加软件的具体信息,然后通过 href 可以直接调用 1.写入注册表信息,注册, ...
- html5中的input和label写法与取值
demo 效果图如上 label是html5特有的,是定义 input 元素的标注.凡是input前面要有个label标识下,label和input真是一对好兄弟啊.<label> 标签的 ...
- mybatis 传入多个参数
一.单个参数: public List<XXBean> getXXBeanList(@param("id")String id); <select id=&quo ...
- CentOS 6.7 配置 yum 安装 nginx
第一步,在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo: cd /etc/yum.repos.d/ vim nginx.repo 填写如下内容: [nginx] na ...
- Kafka Java API获取非compacted topic总消息数
目前Kafka并没有提供直接的工具来帮助我们获取某个topic的当前总消息数,需要我们自行写程序来实现.下列代码可以实现这一功能,特此记录一下: /** * 获取某个topic的当前消息数 * Jav ...
- js替换元素与设置时间间隔
var lastReportTime = 0; //设置时间间隔 window.onload = function(){ setInterval(handleRefresh, 3000); } fun ...
- iOS - UIAlertController三种显示提示框代码
UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView.UIActionSheet 1、UIAlertController显 ...