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 ...
随机推荐
- 为11.2.0.2 Grid Infrastructure添加节点
转自:http://www.askmaclean.com/archives/add-node-to-11-2-0-2-grid-infrastructure.html 在之前的文章中我介绍了为10g ...
- 3. RNN神经网络-LSTM模型结构
1. RNN神经网络模型原理 2. RNN神经网络模型的不同结构 3. RNN神经网络-LSTM模型结构 1. 前言 之前我们对RNN模型做了总结.由于RNN也有梯度消失的问题,因此很难处理长序列的数 ...
- 阿里云ECS服务器主机安装多个网站
web|服务器|站点 Windows 2000 Server安装成功后,一般会启动一个默认的Web站点,为整个网络提供Internet服务.在中小型局域网中,服务器往往只有一台,但是一个Web站点显然 ...
- Ubuntu下安装hbase
1.在清华镜像站点下载hbase的安装文件,选择的是stable的版本,版本号是hbase-1.2.5/ 2.解压放在/usr/local的目录下 3.修改权限 sudo chown -R hduse ...
- Guava学习笔记(三):集合
添加Maven依赖 ListsTest import com.google.common.collect.Lists; import org.hamcrest.core.Is; import org. ...
- Java中创建对象的五种方式
我们总是讨论没有对象就去new一个对象,创建对象的方式在我这里变成了根深蒂固的new方式创建,但是其实创建对象的方式还是有很多种的,不单单有new方式创建对象,还有使用反射机制创建对象,使用clone ...
- JS中lambda表达式的优缺点和使用场景(转)
add by zhj: 最近在看ES6,看到了箭头函数,我个人感觉箭头函数适用于函数体中不用this的匿名函数,在箭头函数中使用this是一个坑 原文:http://ourjs.com/detail/ ...
- [Laravel] 10 - WEB API : wrapper
前言 一.常用的解决方案 React 前端 + PHP (Laravel) 后端 Such as "some exposure to WEB API’s and/or RESTful“. 使 ...
- iOS SQLite 数据库迁移
本文转载至 http://www.jianshu.com/p/c19dd08697bd 最近不得不考虑关于数据库迁移的问题,原先用了种很不好的处理方式(每次版本升级就删除本地数据库,太傻),于是开始考 ...
- 关于vb代码复制到其他地方出现乱码的问题
今天笔者在学习vb编程时,想将自己的一段测试代码记录到云笔记中,方便以后查阅,代码如下: 结果在复制到其他的地方的时候,均出现乱码的现象,主要是针对代码的中的中文,如下效果 Private Sub C ...