[Leetcode] 771. Jewels and Stones

避坑指南:HslCommunication连接西门子PLC时常见的5个错误及解决方法(S7-1200/S1500适用) 本文详细解析了使用HslCommunication连接西门子S7-1200/S1500 PLC时常见的5个错误及解决方法,包括基础连接配置、数据块读写异常、连接闪断、防火墙与权限问题以及高级调试技巧。通过实战案例和代码示例,帮助开发者快速定位和解决连接问题,提升工业自动化项目的开发效率。 阅读详情

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.
Language: C++
解法一:(Accepted)
思路:
遍历S中的字符,分别在J中找看存不存在(用.find(x))

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int result = 0;
        for(int i = 0; i<S.size();i++)
        {
            string c = S.substr(i,1);//取S中位置i开始,长度为1的子字符串
            if(J.find(c)!=string::npos) //重要!J中存在c 
            result++;
        }
        return result;
    }
};
  • 变式:如果description中变成不是case sensitive,即"a"和"A"视为相同,应该怎么办?
  • 用到string type的成员函数:string::substr(i,3)(取位置i开始,长度为3的字符串,且代码中赋给c,c的类型只能是string,不能是char);string::find(c)在字符串里面找子串c,如果返回string::npos说明没找到。

解法二:用set (Accepted)
思路:
由于J中的字母都是distinct的,想到用set容器,把J中的字母都存在Jset中,然后遍历字符串S中每一个字符,如果在Jset中找到,则result++,最后返回。

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int result = 0;
        set<char> Jset(J.begin(),J.end());
        for(char s:S)
            if(Jset.count(s))
                result++;
        return result;
    }
};
  • C++ for循环方法:
  • 传统方法for(int i; i < n; i++){}
  • 基于范围的for循环(C++11)

    //遍历数组
    int array[5] = {1,2,3,4,5};
    for(int &x : array)//定义范围迭代的变量x,以及迭代范围array,如果要对范围内变量进行改动,则要定义引用,如此处
      x++;//每个数组元素+1
    for(auto &x : array)
      x*=2;//auto 类型也是 C++11 新标准中的,用来自动获取变量的类型
    
    //遍历字符串
    string str("Hello Peggy!");
    for(auto &c : str)
      c = toupper(c);//将字符串所有字符变为大写"HELLO PEGGY!"
    
  • vector, set, map的区别
    Map,Set属于标准关联容器,使用了非常高效的平衡检索二叉树:红黑树,他的插入删除效率比其他序列容器高是因为不需要做内存拷贝和内存移动,而直接替换指向节点的指针即可。
    Set和Vector的区别在于Set不包含重复的数据。Set和Map的区别在于Set只含有Key,而Map有一个Key和Key所对应的Value两个元素。
    Map和Hash_Map的区别是Hash_Map使用了Hash算法来加快查找过程,但是需要更多的内存来存放这些Hash桶元素,因此可以算得上是采用空间来换取时间策略。

  • set, hash_set, map, hash_map的区别
    hash_set重点在于set几个字母上,主要实现集合功能,只不过底层函数由hashtable提供,而set是由红黑树作底层,两者的唯一区别就是set默认是排好序的,而hashset不是。
    hashmap重点在于map几个字母上,主要提供对象间的一一映射功能,hashmap底层由hashtable提供,而map底层由红黑树提供,区别同上。
Happy Coding!
Oracle集群管理-19.3升级19.18 opatchauto总结(java报错,resume oneoffs缺失目录) t. (那个认为补丁已经打上了,但是oneoffs目录里面没有。将安装包文件的权限全部改成777.就继续执行了上述命令 ,此时使用opatch lsinv也是报错 ,在正常执行opatchauto apply。1 创建目录,存放补丁包,并赋予权限。于是使用如下命令对补丁进行回滚。 阅读详情

相关推荐

sap 标准委外和工序委外_SAP PP 工序委外详解

在SAP里有外包和外协两种:外包:指把原材料发出去,扣减库存,然后供应商加工生产,最后再作为成品入库。主要是在MM模块下实现的。对应的英语叫Subcontracting。外协:一般是指某种工序外协,在工艺路线里设置某个工序为外协后,系统自动会产生一个采购订单,一般是指采购某种服务。采购回来后再继续本公司的其他工序。主要是在PP模块下实现的。对应的英语叫Processing。2.当有委外加工的业务流...

weixin_26849971的博客 4594

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

Just The Way You Are 418

Modbus 通信协议详解

Modbus是由 Modicon(现为施耐德电气公司的一个品牌)在 1979 年发明的,是全球第一个真正用于工业现场的总线协议。ModBus网络是一个工业通信系统,由带智能终端的可编程序控制器和计算机通过公用线路或局部专用线路连接而成。其系统结构既包括硬件、亦包括软件。它可应用于各种数据采集和过程监控。为更好地普及和推动Modbus在基于以太网上的分布式应用,目前施耐德公司已将Modbus协议的所有权移交给 IDA(,分布式自动化接口)组织,并成立了 Modbus-IDA 组织。

Projectsauron 的博客 7万+

LeetCode之771:Jewels and Stones

1. 解法1 (遍历比较,时间复杂度O(J*S),空间复杂度O(1)) 1.1 C#代码 public class Solution { public int NumJewelsInStones(string J, string S) { int Jlen = J.Length; int Sle...

如切如磋,如琢如磨 468

BugkuCTF-PWN题pwn2-overflow超详细讲解

解题思路 1)计算出get_shell_的地址偏移量 2)算出来之后就直接溢出到后门函数 知识点 x64函数调用规则 解题之前我们先学下linux x64的函数调用规则。 x64机器在调用某个函数前,比如int func(int a, int b, int c, int d, int e, int f, int g, int h),首先他会把前6个参数从左往右存入到寄存器rdi、rsi、rdx、rcx、r8、r9,其余参数存入到栈里,保存的顺序是从右往左入栈。比如abcdef会存入到寄存器里,然后一次入栈h

am_03的博客 3023

cartographer实时显示三维点云地图

cartographer不能实时显示三维点云地图, 这是大家公认的cartographer 3d建图的弊端. 这篇文章就带着大家将cartographer三维地图显示出来. 这部分的代码已经开源在 https://github.com/xiangli0608/cartographer_detailed_comments_ws 1 需要的数据 既然想要显示三维点云地图, 就必须要有每帧数据的点云, 以及这帧点云经过后端优化后的坐标, 这2个数据. 通过阅读代码可知, cartographer中前端是不保存点云

李想的博客 4027

基于AWS CloudTrail日志的云服务事件声化系统设计与实现

在云原生架构中,日志监控是保障系统安全与稳定的关键技术。传统基于文本的日志分析面临信息过载的挑战,难以实时感知异常。通过将结构化日志数据转化为可感知的音频信号,能够利用人类听觉通道实现高效的边缘注意力监控。其技术原理在于构建事件驱动管道,对日志流进行实时过滤、优先级分级与模式识别,并基于声学参数映射生成对应音景。这种数据声化技术为运维可视化提供了创新思路,尤其在实时态势感知和异常直觉发现场景中价值显著。本文以AWS CloudTrail日志为例,结合AI协同设计,详细阐述了如何将云服务API调用事件转化为包

weixin_30628801的博客 667

奇迹mu服务端DATA文件解析:从基础配置到高级玩法

本文深入解析奇迹MU服务端DATA文件的核心配置与高级玩法,涵盖基础设置、怪物AI系统、技能战斗机制、副本事件管理及物品商城定制。通过详细示例和实用技巧,帮助开发者掌握从经验倍率调整到动态事件触发的全方位配置方法,实现个性化的私服搭建与运营。

weixin_29254673的博客 547

[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的博客 3654

windows版vCenter STS证书替换步骤说明

windows版vCenter STS证书,手动更新步骤

技术是手段,服务才是目的。开源与协作是创新的活水,而核心技术的自主可控是立足之本。身处黄金时代,唯持续学习,方能不惧变化。从使用者到创造者,中国技术的未来,正待你们接过火炬,走得更远、更稳、更好。 1048

OpenCV-Python实战(11)——OpenCV轮廓检测相关应用

在计算机视觉领域,轮廓通常指图像中对象边界的一系列点。因此,轮廓通常描述了对象边界的关键信息,包含了有关对象形状的主要信息,该信息可用于形状分析与对象检测和识别。在本文中,我们将学习如何利用获取到的轮廓,进行形状分析以及对象检测和识别。

盼小辉丶的博客 1万+

算法题-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的博客 505

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

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

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Leetcode PHP题解--D1 771. Jewels and Stones

771. Jewels and Stones 由于是按难易度排序的,因此本题是第一题。 题目链接 771. Jewels and Stones 题目分析 从第二个参数S中找第一个参数J 中出现的字符,返回找到的字符个数。 也就是说,第一个参数J是一个需要找的字符的列表。只是拼接成字符串了。 而第二个参数S是被查找的字符串。 简单地说就是要在S里找J。 先把问题简化为J中只有一个字符的情形,因为字符...

weixin_33972649的博客 454

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【Leetcode_easy】771. Jewels and Stones

problem 771.Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, string S) { int res = 0; unordered_set<char> js(J.begin(), J.end()...

weixin_30340617的博客 448

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 stones you have. Each character in S is a type of stone you have. ...

ccystewart的博客 523

python实现 leetcode 771 jewels and stones

说来惭愧,别人几年前干的事情,拖到今天自己才开始自己的leetcode之路。但是开始就好吧!题目描述不再摘抄。AC率最高的一题,一个寒假都没编程的我开始竟然有些懵,参考了discuss里大神lee215的方法得到结果:class solution: def numofJewels(self,J,S): setJ=set(J) return sum(s in setJ for s in S...

大胖子球花的博客 2509

leetcode-771. Jewels and Stones

771. Jewels and Stonesclass Solution: def numJewelsInStones(self, J, S): """ :type J: str :type S: str :rtype: int """ self.J = J self.S = S...

云梦泽 638

LeetCode //C - 771. Jewels and Stones

This problem counts how many stones are also jewels given two strings. The solution uses a lookup table (boolean array) to mark jewel characters for O(1) checks. It iterates through stones, counting matches with jewels. The approach efficiently handles cas

Made in Code 1175

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

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

XTAOTWO的博客 597

Leetcode 771. Jewels and Stones

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

兔子窝 244

LeetCode - 771. Jewels and Stones

在LeetCode的算法世界里,771题"Jewels and Stones"以其简洁的题目描述和多样的解法吸引着众多学习者。本题要求我们统计字符串stones中有多少字符同时出现在字符串jewels中,看似简单的问题背后却蕴含着不同的解题思路和性能优化技巧。

Amon的专栏 595

LeetCode 771. Jewels and Stones

LeetCode 771. Jewels and Stones (Easy) 主要知识点:string;优先级:0

温迪今天刷题了吗 494

【LeetCode题解---771】Jewels and Stones.md

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

程序猿杂货铺 361

LeetCode771: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 sto...

卷卷萌的博客 327

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 stones you have. Each character in S is a type of stone you have. You ...

Wisimer 200
上一篇: [Leetcode] 561. Array Partition I
下一篇: 算法笔记-快速排序(Quick Sort)
Peggy_Chang
博客等级 码龄11年 20粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值