【LeetCode】771. Jewels and Stones 解题报告
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 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
Note:
- S and J will consist of letters and have length at most 50.
- The characters in J are distinct.
题目大意
J里面的每个字符是个宝石,保证不重复。S中的每个字符是一个石头,有可能出现重复。统计有多少个石头恰好也是宝石。
解题方法
数组count
因为J里的元素是独一无二的,所以只要数一数S中出现了多少个j就行了。不需要用set().
时间复杂度是O(MN),空间复杂度是O(1)。M是J长度,N是S长度。
class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
return sum(S.count(j) for j in J)
字典Counter
先用Counter保存每个字母出现的次数,然后由于J里面的字符是不重复的,所以直接遍历,然后统计其中的每个字符在S中出现的次数就行了。
时间复杂度是O(MN),空间复杂度是O(N)。M是J长度,N是S长度。
class Solution:
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
sCount = collections.Counter(S)
res = 0
for j in J:
res += sCount[j]
return res
日期
2018 年 1 月 28 日
2018 年 11 月 2 日 —— 浑浑噩噩的一天
【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 ...
- 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 ...
随机推荐
- wireshatk_teach
wireshark抓包新手使用教程 Wireshark是非常流行的网络封包分析软件,可以截取各种网络数据包,并显示数据包详细信息.常用于开发测试过程各种问题定位.本文主要内容包括: 1.Wiresha ...
- Linux— 查看系统发布版本信息
[root@zf-test-web01-4 ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core)
- Excel-统一小括号格式(中文小括号,英文小括号)
1.统一小括号格式(中文小括号,英文小括号) 公式=ASC("(") #"(" 解释函数: ASC(A1)#对于双字节字符集(DBCS)语言,将全角英文字符(即 ...
- Perl字符串处理函数用法集锦
Perl字符串处理函数 0.函数名 index 调用语法position=index(string,substring,position); 解说返回子串substring在字符串string中的位置 ...
- A Child's History of England.25
It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...
- Spark(一)【spark-3.0安装和入门】
目录 一.Windows安装 1.安装 2.使用 二.Linux安装 Local模式 1.安装 2.使用 yarn模式 1.安装 2.使用 3.spark的历史服务器集成yarn 一.Windows安 ...
- Scala(二)【基本使用】
一.变量和数据类型 1.变量 语法:val / var 变量名:变量类型 = 值 val name:String = "zhangsan" 注意 1.val定义的变量想到于java ...
- Dubbo服务暴露延迟
Dubbo 2.6.5 版本以后,如果我们的服务启动过程需要warmup事件,就可以使用delay进行服务延迟暴露.只需在服务提供者的<dubbo:service/>标签中添加delay属 ...
- 实现nfs持久挂载+autofs自动挂载
实验环境: 两台主机 node4:192.168.37.44 NFS服务器 node2:192.168.37.22 客户端 在nfs服务器,先安装nfs和rpcbind [root@node4 fen ...
- table表格数据无缝循环滚动
分享一个好看的表格无缝滚动:(实战用起来很舒服) 直接copy代码到你的程序中: 1.HTML <div class="tablebox"> ...