Leetcode 771. Jewels and Stones Java解法

LwIP TCP Client 解决长数据发送的困扰 问题描述 LWIP 的 TCP Client 发送数据可以调用 tcp_write() 函数,将数据储存在缓冲区里面,然后等待超时自动发送或者调用tcp_output()函数进行发送。 然而 tcp_write() 函数 需要发送的数据过长时,将无法发送,并返回 ERR_MEM。 tcp_write()函数解析 tcp_write()的函数代码很多,就不细看代码了, 先看tcp_write()的完整函数: err_t tcp_write(struct tcp_pcb *pcb, const void 阅读详情

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

【教程4>第1章>第12节】通过Verilog读取txt格式数据进行测试并将结果导入到matlab 我们在verilog的testbench中,调用txt文件,将上述正弦样本数据作为测试激励输入到verilog模块中。通过这个方法,可以将一些较为复杂的数据导入到verilog中,进行FPGA数据处理模块的开发。即通过w方式打开一个新的txt文件,然后将verilog的输出保存到XY1.txt文件中。这个部分是txt文件的路径,实际使用时,修改为您自己的路径,或者将txt文件放在和我一样的路径中。这个是数据长度,matlab产生的txt多少长度,这个参数就修改为多少长度。这个txt就是保存这段数据的文件。 阅读详情

相关推荐

【云迁移工具RDA】迁移常见问题

Agent无法正常启动,Linux系统提示“SMS.0201 Failed to start sms agent!”,Windows系统提示“SMS.0201 源端主机网络繁忙,无法连接api网关”。该问题通常是由于源端没有通公网,或者没有按照要求配置hosts,无法与iam或者sms通信导致。需要排查源端网络。1.登录源端服务器。2.检查源端是否能正常访问互联网,若无法访问,请处理后重新启动Agent。可参考弹性公网IP Ping不通检查源端的DNS,防火墙,安全组、本地网络等。

2404_89358774的博客 1486

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

基于Ardupilot/PX4固件,APM/PIXhawk硬件的VTOL垂直起降固定翼软硬件参数调试(第一篇)安装调试

基于Ardupilot/PX4固件,APM/PIXhawk硬件的VTOL垂直起降固定翼调试 本文内容大部分来自Kris,我们的K大,在VTOL领域的大牛,在此,非常感谢K大。能引用K大的成果也是自己的荣幸,不便之处还请K大多多包涵,在此引用只是为方便自己日常学习,部分参数表里根据自身情况,增加了相应参数。 1.安装及下载Ardupilot官方固件: VTOL固件的来源可分为Ardupilot官方固...

u011322358的博客 3万+

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

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

weixin_33725722的博客 297

Keil MDK 5.37及之后版本 安装AC5(ARMCC)编译器详细步骤

由于Keil 5.37及之后版本不再默认安装AC5(ARMCC)编译器,这就会导致由AC5编译的工程无法正常编译,往往输出窗口会提示以下信息:*** Target ‘STM32xxxx‘ uses ARM-Compiler ‘Default Compiler Version 5‘ which is not available.

s_Modesto的博客 1万+

FitzHugh-Nagumo 模型连续-MATLAB仿真

【代码】FitzHugh-Nagumo 模型连续-MATLAB仿真。

qq_43688618的博客 884

Java容器使用总结

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

楚兴 2654

【学习系列】SAP RAP 20:Unmanaged场景开发示例-调用BAPI、BAPI包装类、BO接口创建标准采购订单

本文介绍了在SAP RAP(Restful ABAP Programming)框架下,通过三种方式实现采购订单创建的方案。首先分析了RAP框架对传统BAPI调用的限制,特别是禁止显式COMMIT语句的要求。随后详细阐述了三种实现方式:1)直接调用传统BAPI(仅支持OP环境);2)使用BAPI包装类(仅支持OP环境);3)调用标准业务对象接口(支持所有环境)。 文章重点说明了在unmanaged场景下的技术实现细节,包括字段控制、缓冲区管理、测试运行与实际调用的分离处理等。针对每种方式都提供了完整的实施步骤

DeveloperMrMeng的博客 600

jdk源码浅读-HashSet

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

Java爬坑之路 207

osgearth添加经纬度网格,并显示鼠标所指经纬高度信息

一、代码 osgearth官方例子 #include <osg/Notify> #include <osgGA/StateSetManipulator> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgEarth/MapNode> #include <osgEarthUtil/EarthManipulator> #

ljjjjjjjjjjj的博客 3564

AI应用架构师收藏:大规模AI系统AIOps智能运维的4个落地案例

本文精选互联网、金融、制造、电信四大行业的大规模AI系统AIOps落地案例,覆盖云原生AI平台、风控AI系统、工业AI质检、5G网络AI优化等典型场景。行业背景与系统规模:业务目标、AI系统架构、运维挑战;AIOps解决方案设计:数据采集层→分析层→决策层的全栈架构,技术栈选型与理由;核心技术实现细节:异常检测算法、根因分析模型、自动化恢复流程(含Python代码示例);实施效果与经验总结:关键指标对比、踩坑教训、架构师视角的最佳实践。

AI天才研究院 856

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

C#上位机性能优化全攻略:彻底解决界面卡顿+多线程死锁+PLC数据延迟,产线实测稳定运行2年

一开始我们用的是网上找的零散优化代码,根本满足不了要求,后来花了2个月的时间,把之前27条产线的代码整理、重构、优化,做了一套这套方案上线后,连续稳定运行2年零3个月:界面响应时间稳定在50ms以内,多线程死锁率持续为0,PLC数据延迟稳定在150ms以内,系统可用率≥99.99%,帮客户一年省下了超过480万的停线损失、罚款损失和人工成本,客户第二年就把剩下的22条产线也全部交给我们做了,还介绍了3家同行。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 294

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

[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、付费专栏及课程。

余额充值