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 ...
随机推荐
- Haproxy全透明代理
1. 系统环境搭建 操作系统Centos7 内核版本3.10 Centos7已自带TPROXY模块,不需要安装TPROXY 2. Haproxy下载,编译,安装,配置 下载地址 http://www. ...
- TestNG源代码分析:依赖管理的实现
TestNG源代码分析:依赖管理的实现 2018-03-19 1 背景 当case之间有依赖关系,有依赖关系的case,它们的执行顺序是有限制的.TestNG提供了依赖管理功能 2 基础理论 这个执行 ...
- Java知多少(93)鼠标事件
鼠标事件的事件源往往与容器相关,当鼠标进入容器.离开容器,或者在容器中单击鼠标.拖动鼠标时都会发生鼠标事件.java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionL ...
- yizhihongqiang
最新网址:https://www. hongxingwangzhi .com/
- 【转】Android开发教程 --- Android调用WS
原文地址:http://www.cnblogs.com/jasoncc/archive/2011/12/23/2297950.html Hi,大家好! 上节我们搭建了Java版的WS,那么在Andro ...
- 自windows8以后,所有版本(专业版、企业版、旗舰版)都支持从 vhd 启动
而在windows 7 中,只有:企业版.旗舰版: 支持 从 vhd 启动!
- glob通配符
描述glob是shell使用的路径匹配符,类似于正则表达式,但是与正则表达式不完全相同.在linux操作中如文件匹配等等其实已经使用了glob通配符.由于其在路径匹配方面的强大,其他语言也有相应的实现 ...
- [Algorithm] Maximum Flow
Ref MIT: lecture-13-incremental-improvement-max-flow-min-cut/ Ford Fulkerson algorithm for finding m ...
- 修改web前端访问端口
说明: URL规则可添加变量部分,也就是说将符合同种规则的URL抽象成一个URL模式 1 2 3 @app.route('/instance/<uuid>/') def instance( ...
- iOS SQLite 数据库迁移
本文转载至 http://www.jianshu.com/p/c19dd08697bd 最近不得不考虑关于数据库迁移的问题,原先用了种很不好的处理方式(每次版本升级就删除本地数据库,太傻),于是开始考 ...