LeetCode 771: 宝石与石头 Jewels and Stones
题目:
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
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.
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
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".
示例 1:
输入: J = "aA", S = "aAAbbbb"
输出: 3
示例 2:
输入: J = "z", S = "ZZ"
输出: 0
注意:
S和J最多含有50个字母。J中的字符不重复。
Note:
SandJwill consist of letters and have length at most 50.- The characters in
Jare distinct.
解题思路:
J 改为 Set 集合, 遍历 S 即可(因为 Set 查找复杂度为常数)
Java:
class Solution {
public int numJewelsInStones(String J, String S) {
Set<Character> set = new HashSet<>();
for (char c : J.toCharArray())
set.add(c);
int count = 0;
for (char c : S.toCharArray())
if (set.contains(c)) count++;
return count;
}
}
Python:
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
count = 0
hash_set = set(J)
for c in S:
if c in hash_set:
count += 1
return count
欢迎关注微.信.公..众号: 爱写Bug

LeetCode 771: 宝石与石头 Jewels and Stones的更多相关文章
- [Leetcode 771]宝石和石子 Jewels and Stones HashSet简单应用
[题目] You're given strings J representing the types of stones that are jewels, and S representing the ...
- Java实现 LeetCode 771 宝石与石头(这是真暴力)
771. 宝石与石头 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [LeetCode]771. 宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- LeetCode 771. 宝石与石头(java)
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- LeetCode 771 宝石和石头
Input: J = "aA", S = "aAAbbbb" Output: 3 解:J为宝石字符串,S为包含宝石的字符串,J中的字母如果在S中出现数字就➕1 ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
- 力扣(LeetCode) 771. 宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- leetcode-解题记录 771. 宝石与石头
题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符 ...
随机推荐
- docker-compose编排参数详解
一.前言 Compose是一个用于定义和运行多容器Docker应用程序的工具.使用Compose,您可以使用YAML文件来配置应用程序的服务.然后,使用单个命令,您可以从配置中创建并启动所有服务. C ...
- Xcode 三方库管理工具 CocoaPods 的安装流程
1. 移除现有 Ruby 默认源: $ gem sources --remove https://rubygems.org/ 2. 使用新的 Ruby 源: $ gem sources -a http ...
- JS---变速动画函数封装
变速动画函数封装 匀速动画:每次步数都是10 (var step=10;) 变速(缓动)动画:每次的步数是用当前位置和目标位置相减 var step=(target-current)/10; 代码如下 ...
- 第01讲 Android开发系列---Activity
一. Android系统版本及详细信息 最新数据 https://developer.android.com/about/dashboards/ 二. Android项目初探 1. 使用a ...
- Python—Celery 框架使用
一.Celery 核心模块 1. Brokers brokers 中文意思为中间人,在这里就是指任务队列本身,接收生产者发来的消息即Task,将任务存入队列.任务的消费者是Worker,Brokers ...
- gdisk转fdisk
分区测试的时候发现之前用gdisk分区之后,就无法用fdisk进行分区了,哪怕格式化了也不行,通过fdisk 查看硬盘,发现硬盘都变成了GPT分区,无法通过fdisk进行分区操作,所以要通过parte ...
- 用 Keras 实现单词级的 one-hot 编码 & 使用散列技巧的单词级的 one-hot 编码
from keras.preprocessing.text import Tokenizer samples = ['The cat sat on the mat.', 'The dog ate my ...
- SpringBoot整合jdbcTemplate
一.目录展示 二.导入依赖 三.配置文件 四.Student实体类 package com.zn.entity; public class Student { private Integer stu_ ...
- Password Management:Hardcoded Password 密码管理:硬编码密码
- Fabric-Ca使用
Fabric-Ca的概念不再解释了,这里只说明使用方法: 前置条件 Go语言1.10+版本 GOPATH环境变量正确设置 已安装libtool和libtdhl-dev包 Ubuntu系统 通过以下命令 ...