博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
774. Jewels and Stones
阅读量:7113 次
发布时间:2019-06-28

本文共 676 字,大约阅读时间需要 2 分钟。

题目链接

思路

从题目得知,我们是求字符串J在字符串S中出现的次数。也就是说,one-pass就可以brute force获得答案。
当然可以利用set()数据结构进行优化。

算法复杂度

时间:O(M*N) or O(M + N) where M is the length of J and N is the length of S空间:O(1) or O(M) where M is the length of J

代码

class Solution(object):    def numJewelsInStones(self, J, S):        """        :type J: str        :type S: str        :rtype: int        """        count = 0        for s in S:            if s in J:                count +=1        return count

用set()优化:

class Solution(object):    def numJewelsInStones(self, J, S):        """        :type J: str        :type S: str        :rtype: int        """        setJ = set(J)        return sum(s in setJ for s in S)

转载地址:http://ccwel.baihongyu.com/

你可能感兴趣的文章
MS ASP.Net Ajax 服务端扩展
查看>>
android102 查询,插入联系人
查看>>
数据库邮件
查看>>
adstrtal.sh报超时错误 ERROR : Timed out( 100000 ): Interrupted Exception
查看>>
一个前端工程师的基本修养
查看>>
ZT:三十个好习惯
查看>>
.Net开发笔记(七)使用组件编程
查看>>
ASP.NET企业开发框架IsLine FrameWork系列之八--AppLogProvider日志框架(下)
查看>>
DataBase异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间
查看>>
一个android版本的rss阅读器--明天补充实现过程,先上图
查看>>
WPF TreeView
查看>>
HTML: 仿写一个财经类静态的网页
查看>>
C#读写config配置文件
查看>>
JavaScript:文本域事件处理
查看>>
关于dctser进程
查看>>
一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇-演示ORM中的查询
查看>>
win7远程登录
查看>>
5.6. DHCP
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->Web版本模块管理界面新增模块排序功能...
查看>>
ajax与算法,sql的group处理
查看>>