LeetCode:771. Jewels and Stones

LeetCode 771. 宝石与石头Jewels and Stones 题目描述 阅读详情

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".

Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:
Input: J = "z", S = "ZZ"
Output: 0

其实就是统计J中的字符在S中的出现的次数。

思路一:暴力求解

双重循环,不再赘述。Python 代码实现

或者内部循环里直接使用 in 关键词:

class Solution:
    def numJewelsInStones(self, J: str, S: str) -> int:
        lS = len(S)
        countJ = 0
        for i in range(lS):
            if (S[i] in J):
                countJ+=1
        return countJ
思路二:使用ASCII码的哈希方法

小写字符 a 的ASCII码为97,大小写字母一共58个。所以要用到一个长度为58的整型数组,如果J字符串中还有某个字符,则对应的位置设为1。然后遍历一次字符串S,如果某个位置的字符对应的哈希值为1,则表明存在于J中,个数加1。

Python 代码实现

class Solution:
    def numJewelsInStones(self, J: str, S: str) -> int:
        lJ = len(J)
        lS = len(S)
        charMap = [0]*58
        for i in range(lJ):
            charMap[ord(J[i])-97]=1
        countJ = 0
        for i in range(lS):
            if (charMap[ord(S[i])-97] == 1):
                countJ+=1
        return countJ

THE END.

Widar3.0复现日志(DFSExtractionCode) 需要使用的matlab基本功能 阅读详情

相关推荐

性能测试软件(LR12) 具体汉化(中文)方法

LoadRunner 12 的下载及安装详细过程请参考:https://blog.csdn.net/xiao66guo/article/details/105465854 1、在LoadRunner文件中找到 Setup.exe 可执行文件,并以管理员的身份运行: C盘 —> Temp—>HP LoadRunner 12.02 Community Edition—> D...

多谢大人光临寒舍,请随便看看,喝口茶吧 ☕️☕️☕️ 1994

LeetCode刷题:771. Jewels and Stones

LeetCode刷题:771. Jewels and Stones 原题链接:https://leetcode.com/problems/jewels-and-stones/ You're given stringsJrepresenting the types of stones that are jewels, andSrepresenting the stones you hav...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 639

Type-C连接中的CC引脚:Rp与Rd电阻的识别与供电模式选择

本文深入解析Type-C接口中CC引脚的核心功能,包括Rp与Rd电阻的识别原理及供电模式选择机制。通过详细的分压计算和实战案例,揭示Type-C设备如何通过CC引脚实现方向检测、角色识别和供电协商,为硬件设计提供关键指导。

weixin_30616969的博客 348

Leetcode PHP题解--D1 771. Jewels and Stones

771. Jewels and Stones 由于是按难易度排序的,因此本题是第一题。 题目链接 771. Jewels and Stones 题目分析 从第二个参数S中找第一个参数J 中出现的字符,返回找到的字符个数。 也就是说,第一个参数J是一个需要找的字符的列表。只是拼接成字符串了。 而第二个参数S是被查找的字符串。 简单地说就是要在S里找J。 先把问题简化为J中只有一个字符的情形,因为字符...

weixin_33972649的博客 454

Cesium高效渲染海量GeoJSON数据的7种优化策略与实战解析

本文深入解析Cesium高效渲染海量GeoJSON数据的7种优化策略,包括瓦片化转换、LOD分级加载、数据简化等关键技术。通过实战案例展示如何解决渲染卡顿问题,提升矢量数据加载性能,实现流畅的60FPS交互体验。特别针对GeoJSON大数据量场景提供完整优化方案。

h0i1j2k3l的博客 942

大彩串口屏+LUA避坑指南:从Flash读写到控件状态配置的5个常见问题

本文针对大彩串口屏的LUA脚本开发,深度剖析了Flash数据存储与控件状态配置中的5个常见陷阱。重点讲解了如何通过标志位安全初始化Flash、正确处理多字节数据转换,以及如何精准配置按钮与文本控件的状态与事件响应,帮助开发者提升开发效率与代码可靠性。

weixin_27945229的博客 251

【Leetcode_easy】771. Jewels and Stones

problem 771.Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, string S) { int res = 0; unordered_set<char> js(J.begin(), J.end()...

weixin_30340617的博客 448

YOLOv8自定义训练:车牌识别全流程(从标注到推理,附可视化代码)

车牌检测全流程实践指南 摘要:本文详细介绍了基于YOLOv8的车牌检测完整实现方案。针对车牌检测中的三大难点(小目标、多角度、光照变化),提供了从数据准备到模型部署的全流程解决方案。重点包括:1)数据采集与标注规范;2)针对小目标的YOLOv8模型优化配置;3)K-means锚框聚类方法;4)训练参数调优策略。通过实际验证,该方法可获得95%以上的检测准确率,并附有完整的代码实现和可视化工具,便于开发者快速复现应用。 (字数:149字)

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 856

Leetcode学习笔记:#771 Jewels and Stones

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. ...

ccystewart的博客 523

【RT-DETR改进涨点】ResNet18、34、50、101等多个版本移植到ultralytics仓库(RT-DETR官方一比一移植)

本文是本专栏的第一篇改进,我将RT-DETR官方版本中的ResNet18、ResNet34、ResNet50、ResNet101移植到ultralytics仓库,网上很多改进机制是将基础版本的也就是2015年发布的ResNet移植到ultralytics仓库中,但是其实RT-DETR实验的ResNet和其是有很大区别的在其中有很多细节上的不同(文章中我也会列出举例)

Snu77的博客 7240

【逐函数讲解ORB_SLAM3源码】获取普通帧率、关键帧、IMU数据

获得imu的旋转 mRwc * mImuCalib.mTcb.rotationMatrix(); Sophus::SE3 mTwc 创建ORB_SLAM3系统 Create SLAM system. It initializes all system threads and gets ready to process frames. ORB_SLAM3::System SLAM(argv[1],argv[2],ORB_SLAM3::System::IMU_RGBD, true, 0, file_nam

小秋 AI SLAM 入门实战教程 1264

python实现 leetcode 771 jewels and stones

说来惭愧,别人几年前干的事情,拖到今天自己才开始自己的leetcode之路。但是开始就好吧!题目描述不再摘抄。AC率最高的一题,一个寒假都没编程的我开始竟然有些懵,参考了discuss里大神lee215的方法得到结果:class solution: def numofJewels(self,J,S): setJ=set(J) return sum(s in setJ for s in S...

大胖子球花的博客 2509

DW3000芯片驱动API介绍

详细介绍DW3000驱动程序API的调用流程说明和中断处理程序流程说明

DT程序员的博客 1534

leetcode-771. Jewels and Stones

771. Jewels and Stonesclass Solution: def numJewelsInStones(self, J, S): """ :type J: str :type S: str :rtype: int """ self.J = J self.S = S...

云梦泽 638

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode - 771. Jewels and Stones

在LeetCode的算法世界里,771题"Jewels and Stones"以其简洁的题目描述和多样的解法吸引着众多学习者。本题要求我们统计字符串stones中有多少字符同时出现在字符串jewels中,看似简单的问题背后却蕴含着不同的解题思路和性能优化技巧。

Amon的专栏 595

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LeetCode //C - 771. Jewels and Stones

This problem counts how many stones are also jewels given two strings. The solution uses a lookup table (boolean array) to mark jewel characters for O(1) checks. It iterates through stones, counting matches with jewels. The approach efficiently handles cas

Made in Code 1175

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

(java)leetcode771 宝石与石头(Jewels and Stones)

题目描述: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例1 : Input: J = "aA", S = "aAAbbbb" Output: 3 示例2: Input:...

XTAOTWO的博客 597

Leetcode 771. Jewels and Stones

Question 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 o...

兔子窝 244

LeetCode 771. Jewels and Stones

LeetCode 771. Jewels and Stones (Easy) 主要知识点:string;优先级:0

温迪今天刷题了吗 494

[Leetcode从零开刷]771. Jewels and Stones

Jewels and Stones leetcode 题目 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. ...

legalhighhigh的博客 3654

[Leetcode] 771. Jewels and Stones

Description: 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 m...

Peggy_Chang的博客 641

【LeetCode题解---771】Jewels and Stones.md

【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....

程序猿杂货铺 361

leetcode 771. Jewels and Stones(python)

描述 You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. Letters ar

王大呀呀的博客 703

LeetCode 771. Jewels and Stones(Java实现)

https://leetcode.com/problems/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 Sis a type of stone...

SeanXuu的博客 365

LeetCode771: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 sto...

卷卷萌的博客 327

【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 m

小拳头的博客 947
上一篇: LeetCode:75. Sort Colors
下一篇: LeetCode:617. Merge Two Binary Trees
XP-Code
博客等级 码龄12年 113粉丝 194原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值