【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 ...
随机推荐
- n组字母和最大
字母A-J,用0-9对应字母使得n组数据和最大,输入字符串前面保证非0 如输入组数据: 2 ABC BCA 输出: 1875 思路:其实就是求和,对应字符乘以相应的量级,按系数排序 如上MAX(101 ...
- flask分页功能:基于flask-sqlalchemy和jinja2
先看源码: @app.route('/movie', methods=['GET', 'POST']) @app.route('/home', methods=['GET', 'POST']) @ap ...
- Netty | 第1章 Java NIO 网络编程《Netty In Action》
目录 前言 1. Java 网络编程 1.1 Javs NIO 基本介绍 1.2 缓冲区 Buffer 1.2 通道 Channel 1.3 选择器 Selector 1.4 NIO 非阻塞网络编程原 ...
- 转 【Android】- Android与html5交互操作
转自:https://blog.csdn.net/baidu_35701759/article/details/70314812 1. Android提供了WebView控件可访问网页 通过webVi ...
- 3.5 Rust Generic Types, Traits, and Lifetimes
Every programming language has tools for effectively handling the duplication of concepts. In Rust, ...
- 01 nodejs MVC gulp 项目搭建
文本内容 使用generator-express创建nodejs MVC DEMO 使用gulp实时编译项目 npm安装二进制包,无须再编译wget https://nodejs.org/dist/v ...
- node环境变量配置
1.Node.js 官方网站下载:https://nodejs.org/en/ 2.打开安装,傻瓜式下一步即可,然后配置环境变量 3.因为在执行例如npm install webpack -g等命令全 ...
- Can we use function on left side of an expression in C and C++?
In C, it might not be possible to have function names on left side of an expression, but it's possib ...
- Java实现邮件收发
一. 准备工作 1. 传输协议 SMTP协议-->发送邮件: 我们通常把处理用户smtp请求(邮件发送请求)的服务器称之为SMTP服务器(邮件发送服务器) POP3协议-->接收邮件: 我 ...
- 通过spring-data-redis操作Redis
一.操作String类型数据 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/ ...