[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
Note:
SandJwill consist of letters and have length at most 50.- The characters in
Jare distinct.
给两个字符串,珠宝字符串J和石头字符串S,其中J中的每个字符都是珠宝,S中的每个字符都是石头,区分字母的大小写,问我们S中有多少个珠宝。
Java:
public int numJewelsInStones(String J, String S) {
return S.replaceAll("[^" + J + "]", "").length();
}
Java:
public int numJewelsInStones(String J, String S) {
int res = 0;
Set setJ = new HashSet();
for (char j: J.toCharArray()) setJ.add(j);
for (char s: S.toCharArray()) if (setJ.contains(s)) res++;
return res;
}
Python:
def numJewelsInStones(self, J, S):
return sum(map(J.count, S))
Python:
def numJewelsInStones(self, J, S):
return sum(map(S.count, J))
Python:
def numJewelsInStones(self, J, S):
return sum(s in J for s in S)
Python:
def numJewelsInStones(self, J, S):
setJ = set(J)
return sum(s in setJ for s in S)
Python:
# Time: O(m + n)
# Space: O(n)
class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
lookup = set(J)
return sum(s in lookup for s in S)
Python: wo
class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
res = 0
for i in S:
if i in J:
res += 1 return res
C++:
int numJewelsInStones(string J, string S) {
int res = 0;
unordered_set<char> setJ(J.begin(), J.end());
for (char s : S) if (setJ.count(s)) res++;
return res;
}
All LeetCode Questions List 题目汇总
[LeetCode] 771. Jewels and Stones 珠宝和石头的更多相关文章
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- 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] 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珠宝数组和石头数组中的字母对应
[抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...
- 【LeetCode】Jewels and Stones(宝石与石头)
这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...
- LeetCode 771 Jewels and Stones 解题报告
题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
随机推荐
- python笔记42-http请求命令行工具(httpie)
前言 通常我们需要快速的测试某个接口通不通,一般linux上用curl去发http请求,但是这个命令行工具语法有点复杂了,不够直观. python有一个给人类使用的requests库,非常的简单方便. ...
- Springboot整合通用mapper
通用Mapper的分享使用 参考博客 Mybatis的通用mapper和Hibernate一样都实现了JPA接口,简化了数据库的操作 和Hibernate的对比 Hibernate和Mybatis都是 ...
- Git学习笔记--历史与安装(一)
声明:今天起学习Git,第一篇学习笔记主要借鉴廖雪峰先生的个人博客,以及自己的实践所得. “本教程只会让你成为Git用户,不会让你成为Git专家”——引自廖雪峰博客. 一.Git简介 Git是目前世界 ...
- Top 20 IoT Platforms in 2018
https://internetofthingswiki.com/top-20-iot-platforms/634/ After learning what is the internet of th ...
- c++中 string类型 转为 char []类型
将string类型转换为字符数组char [] char arr[50]; //数组大小根据s的大小确定 string s= "12slfjksldkfjlsfk"; int le ...
- hibernate之多对多关系
hibernate的多对多hibernate可以直接映射多对多关联关系(看作两个一对多) 下面我们拿三张表来做实例 t_book_hb t_book_category_hb(桥接表) t_catego ...
- JMX脚本在某些机器上报错,有的运行超时
运行超时的 是因为在server端运行命令执行脚本,是server给agent下达的指定,但是server端到agent的10050端口没开,所以或一致堵死在那,知道执行超时, 解决:开通server ...
- 定量变量和定性变量的转换(Transform of Quantitative & Qualitative Variables)
定量变量(Quantitative Variables):也称为数值型变量(Numerical Variables),可以用连续值或离散值表示.比如:气温(连续值),学生人数(离散值). 为什么要对定 ...
- Loj刷题记录
又是一年云参营. 所以一起刷省选题吧. LOJ2028 「SHOI2016」随机序列 题目链接. 简要社论 发现+和-可以互相抵消,于是有贡献的时候一段前缀的乘积.设\(s[i]=\prod_{j=1 ...
- 怎么样使element ui 的table某列变色
第一步.在el-table里面加上:row-style="rowClass" <el-table :data="targetCarList" border ...