题目要求

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 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 解题报告的更多相关文章

  1. 【LeetCode】771. Jewels and Stones 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...

  2. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  3. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  4. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  5. [LeetCode] 771. Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  6. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

  9. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

随机推荐

  1. js的new Date()日期的使用

    <script type="text/javascript"> //js获取某个月的天数 function days(year,month){ var dayCount ...

  2. Python测试DB2连通性

    Python测试数据库连通性: #!/usr/bin/python27 #encoding: utf-8 import ibm_db import os import sys def find_db( ...

  3. 使用python抓取58手机维修信息

    之前在ququ的博客上看到说 python 中的BeautifulSoup 挺好玩的,今天下午果断下载下来,看了下api,挺好用的,完了2把,不错. 晚上写了一个使用python抓取58手机维修信息的 ...

  4. 【emWin】例程二十七:窗口对象——Listview

    简介: LISTVIEW小工具可在具有多个列的列表中选择某个元素.由于LISTVIEW小工具包含了 一个HEADER小工具,因此可对列加以管理(排序等).所创建的LISTVIEW 既可以无环绕型框架窗 ...

  5. too many open files

    压测遇到这个问题,每次都查,记录一下: 系统分配文件数太少,临时修改方案: ulimit -n 2048 永久配置: vim /etc/security/limits.conf 底部配置: # End ...

  6. FilenameFilter总结

    一.FilenameFilter介绍   java.io.FilenameFilter是文件名过滤器,用来过滤不符合规格的文件名,并返回合格的文件: 一般地: (1)String[] fs = f.l ...

  7. (笔记)AT91SAM9260的启动过程详细解说

    Bootstrap的启动过程 一. 说明: Bootstrap启动代码是官方提供的一级启动代码,包括汇编和C语言两部分组成.对AT91SAM9260来说编译完成后,代码长度必须小于4KB,烧写到dat ...

  8. git解决 remote: Permission to wuheng1991/site-manager.git denied to XXX

    1.问题 2.解决 生成一个新的SSH KEY ssh-keygen  -t rsa –C “youremail@example.com” 命令: 3.修改 .git/config中的url 4.gi ...

  9. [JS] Topic - define "class" by tricky methods

    Ref:Javascript定义类(class)的三种方法 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(O ...

  10. NUC972 linux 烧录

    节介绍如何刻录uboot.kernel和文件系统到NAND Flash, 并且设定NUC970系列芯片从NAND Flash中开机.本节操作需要windows环境下进行.(初次连接电脑需要安装驱动) ...