Leetcode 771. Jewels and Stones Java解法

Linux内核模块开发实战指南(2025版) 2025年Linux内核模块开发迎来革命性突破!本文将从零开始手把手教你构建可动态加载的AI加速驱动模块,揭秘最新内核特性与开发技巧,更包含独家性能调优方案与跨架构移植秘籍。文末附赠价值百万的调试脚本,助你轻松突破开发瓶颈! 阅读详情

You're given strings J representing the types of stones that are jewels, and Srepresenting 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.

---------------------------------------------------------------------------------------------------------------------------

Approach #1: Brute Force 两个for循环搞定

class Solution {
        public int numJewelsInStones(String J, String S){
            int count = 0;
            for (char s: S.toCharArray())
                for (char j: J.toCharArray())
                    if(j == s){
                        count+=1;
                        break;
                    }
            return count;
        }
}

Complexity:

Time: O(J.length * S.length)
Space: O(J.length * S.length), because of the creation of new arrays.(in Java, Strings cannot be modified.)

知识点:

1. Java对String的处理,使用String.toCharArray(),可以把String转化为一个char的数组。

2. 记住这里这种for循环的写法。

 

Approach #2: Hash Set 

class Solution {
    public int numJewelsInStones(String J, String S) {
        Set<Character> Jset = new HashSet();  // declare a Set<element>
        for (char j: J.toCharArray())
            Jset.add(j);                      // add elements to Set

        int ans = 0;
        for (char s: S.toCharArray())
            if (Jset.contains(s))            // Object.contains()
                ans++;
        return ans;
    }
}

Complexity:

Time: O(J.length+S.length), there are two seperate for loops.

Space: O(J.length), because we created a Set object of J.length.

知识点:

1. HashSet的声明方法及Set的一些method的使用,Object.add(), Object.contains().

boolean add(E e)

Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.

Specified by:

add in interface Collection<E>

Parameters:

e - element to be added to this set

Returns:

true if this set did not already contain the specified element

Throws:

UnsupportedOperationException - if the add operation is not supported by this set

ClassCastException - if the class of the specified element prevents it from being added to this set

NullPointerException - if the specified element is null and this set does not permit null elements 

IllegalArgumentException - if some property of the specified element prevents it from being added to this set

 

boolean contains(Object o)

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).

Specified by:

contains in interface Collection<E>

Parameters:

o - element whose presence in this set is to be tested

Returns:

true if this set contains the specified element

Throws:

ClassCastException - if the type of the specified element is incompatible with this set (optional)

NullPointerException - if the specified element is null and this set does not permit null elements (optional)

boolean remove(Object o)

Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Specified by:

remove in interface Collection<E>

Parameters:

o - object to be removed from this set, if present

Returns:

true if this set contained the specified element

Throws:

ClassCastException - if the type of the specified element is incompatible with this set (optional)

NullPointerException - if the specified element is null and this set does not permit null elements (optional)

UnsupportedOperationException - if the remove operation is not supported by this set

20、超声多普勒成像技术全解析 本文全面解析了超声多普勒成像技术,涵盖彩色血流成像(CFI)、能量多普勒成像、超快多普勒成像和矢量多普勒成像的原理、流程、优势及临床应用场景。详细探讨了各成像模式的特点,如CFI的血流方向与速度可视化、能量多普勒对小血管的高灵敏度、超快多普勒的高帧率分析能力,以及矢量多普勒消除角度依赖性的创新方法。同时分析了技术面临的挑战及未来发展趋势,包括更高分辨率、人工智能辅助诊断和便携式设备的发展,为医学影像研究和临床诊断提供重要参考。 阅读详情

相关推荐

开关电源补偿设计避坑手册:为什么你的Buck电路总在轻载时振荡?

本文深入剖析了Buck电路在轻载时发生振荡的根本原因,并提供了专业的补偿设计避坑指南。文章指出,轻载下输出滤波器Q值飙升、DCM模式切换导致的小信号模型剧变是主要诱因,并详细对比了Type II与Type III补偿网络在宽负载范围内的表现差异。通过结合LTspice仿真与工程实践,指导工程师如何优化补偿环路设计,确保电源从满载到空载的全程稳定。

hadoop5ranger的博客 675

java.util.Set翻译

 Overview Package  Class Use Tree Deprecated Index Help JavaTM 2 PlatformStd. Ed. v1.4.2  PREV CLASS   NEXT CLASSFRAMES    NO FRAMES    <!-- if(window==top) { document.writeln(All Classes)

2257

【毕设】基于LoRa的物联网控制器:从原理图到PCB的完整硬件设计指南

本文提供了一份完整的基于LoRa的物联网控制器硬件设计指南,涵盖从系统框图、原理图设计到PCB布局布线的全流程。详细讲解了电源电路、单片机最小系统、信号接口以及LoRa无线模块等核心模块的设计要点与抗干扰技巧,并分享了实用的调试心得,旨在帮助电子、物联网专业学生顺利完成毕业设计,掌握低功耗远距离物联网节点的硬件开发实践。

weixin_42126677的博客 307

Set集合中去除重复基于值进行判断

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33725722的博客 297

大模型选型实战指南|教育行业:108个模型「闭卷考试」实测,这3类应用场景直接抄作业!

教育行业大模型选型必读 1.闭卷考试!独家评测集:用最新K12真题(小学-高中全学科)构建“闭卷考场”,108个模型同台竞技,数据污染率<5%(传统评测集污染率超60%) 2.价格透明对比:40元/M tokens的阿里系模型VS 2元/M tokens的字节系、腾讯系模型,谁更值? 3.四大选型禁区:>60元但得分<75的模型被列入不推荐名单(附避坑清单) 4.核心结论直出:教学核心环节必须用专用模型,90%场景用通用模型可降本50%+

easyllm的博客 1523

JPA - @MappedSuperclass - 基类注解

目录前言代码示例 前言 @MappedSuperclass注解一般作为基类的注解,表眀该类是映射的超类,继承于它的子类实体在映射时会自动添加到子类实体的对应数据库表中,用于共同的属性代码复用和模型分离。 代码示例 BaseEntity.java import lombok.Getter; import lombok.Setter; import javax.persistence.MappedSuperclass; import java.util.Date; @Getter @Setter @Ma

Maggieq8324的博客 2066

Java容器使用总结

Collection  ├List  │├LinkedList  │├ArrayList  │└Vector  │ └Stack  ├Queue │├Deque │└LinkedList └Set    ├SortedSet    ├TreeSet   └HashSet Map  ├Hashtable  ├HashMap

楚兴 2654

实战指南:基于ELK与Grafana构建天融信防火墙日志可视化看板

本文详细介绍了如何基于ELK与Grafana构建天融信防火墙日志可视化看板,包括环境准备、日志收集与解析配置、Elasticsearch存储优化、Grafana看板设计等关键步骤。通过实战案例和优化技巧,帮助用户高效处理天融信防火墙日志,实现安全态势的可视化监控与分析。

weixin_34246529的博客 226

jdk源码浅读-HashSet

通过阅读源码发现,HashSet底层的实现源码其实就是调用HashMap的方法实现的,所以如果你阅读过HashMap或对HashMap比较熟悉的话,那么阅读HashSet就很轻松,也很容易理解了。我之前也写了一篇关于hashMap源码阅读的文章,可以点击这里查看。 使用过HashSet的都清楚它保存的元素是不可以重复的,其实HashSet的元素都是保存在H...

Java爬坑之路 207

Triton server的部署、构建、backend插件机制代码梳理、模型加载、自定义C++backend流程

Triton inference server的部署、构建、backend机制代码详细梳理、模型加载过程、自定义C++backend流程.

C/C++、嵌入式开发、深度学习算法、模型部署与推理优化 473

UniRx之基本语法格式

想要更好的去学习UniRx,我们最好是先理解UniRx 的语法格式。就像我们去学习英语或者汉语一样,理解了基本的语法规则后,再学其他的就信手拈来了。

TxNet 924

leetcode771题

题目: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 ...

weixin_42069523的博客 227

手把手教你学Simulink--基于飞行控制与稳定性的场景实例:固定翼无人机的自动起飞与降落控制

本文介绍了使用Simulink对固定翼无人机自动起飞与降落控制系统进行建模与仿真的完整流程。首先构建了包含飞行器动力学模型、控制系统、环境模拟等模块的系统结构,详细阐述了简化动力学方程的推导和实现。重点讲解了PID控制器设计,包括起飞阶段的速度控制和降落阶段的高度控制策略。通过添加环境干扰和输入信号,建立了完整的仿真模型,并利用输出显示模块评估系统性能。该实例为无人机自主控制系统的开发提供了有效的仿真验证方法,有助于优化参数提高飞行稳定性。

MHD0815的博客 4311

LeetCode 771. Jewels and Stones--Java和Python解法--简单

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 ...

个人博客 490

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode刷题:771. Jewels and Stones

LeetCode刷题:771. Jewels and Stones 原题链接:https://leetcode.com/problems/jewels-and-stones/ You're given stringsJrepresenting the types of stones that are jewels, andSrepresenting the stones you hav...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 630

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

1. Jewels and Stones (宝石与石头)

Jewels and Stones leetcode 771 题目 You're given stringsJrepresenting the types of stones that are jewels, andSrepresenting the stones you have. Each character inSis a type of stone you have....

yk950309的博客 767

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[Leetcode从零开刷]771. Jewels and Stones

Jewels and Stones leetcode 题目 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. ...

legalhighhigh的博客 3653

LeetCode 771. Jewels and Stones(Java实现)

https://leetcode.com/problems/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 Sis a type of stone...

SeanXuu的博客 363

Leetcode 771. Jewels and Stones

Leetcode 771. Jewels and Stones 一个字符代表一种是宝石(Jewels),一串(Stones)字符串有多少个宝石。 Letters are case sensitive. 区分大小写。 For example: “aA” “abAaAAbbbb” return 5 Approach #1: Brute Force [Accepted] class Solution { public int numJewelsInStones(String J, String S) {

weixin_43876597的博客 150

(java)leetcode771 宝石与石头(Jewels and Stones)

题目描述: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例1 : Input: J = "aA", S = "aAAbbbb" Output: 3 示例2: Input:...

XTAOTWO的博客 589

LeetCode-771. 宝石与石头(Jewels and Stones)

771. 宝石与石头 Difficulty: 简单 更多LeetCode答案欢迎大家关注我的 Github 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1: 输入: J = "a...

精进编程の博客 274

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 ...

不积跬步,无以至千里 504

算法题-leetcode-771 Jewels and Stones

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 Sis a type of stone you have.  You want ...

sinat_22344233的博客 504

leetcode 771. Jewels and Stones

  在csdn上看了那么久别人的博客,今天也开始开通自己的博客了。就算是作为找工作路上的一个总结。有些代码放在eclipse上写过就算了,就算有问题也很难再去翻出来看,还不如放在这里,以后也方便查看。不去面试不知道自己的水平有多渣渣,今天开始刷leetcode以及昨天网易的笔试题,都是在eclipse上运行没毛病最后还是不能完全accept。出现的error还没找到解决方法,放在这里以后也许有一天...

yyatt的博客 281

【Leetcode】771. Jewels and Stones

题目地址: https://leetcode.com/problems/jewels-and-stones/ 给定两个字符串jjj和sss,问sss里有多少个字符是含在jjj里的。 代码如下: import java.util.HashSet; import java.util.Set; public class Solution { public int numJewelsInStones(String jewels, String stones) { Set<Charac

数学、算法爱好者的博客 279

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...

指尖飞舞 536
下一篇: Leetcode 1108. Defanging an IP Address, Java解法
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值