码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 771. Jewels and Stones


    You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

    Letters are case sensitive, so "a" is considered a different type of stone from "A".

    吧

    Example 1:

    Input: jewels = "aA", stones = "aAAbbbb"
    Output: 3
    

    Example 2:

    Input: jewels = "z", stones = "ZZ"
    Output: 0
    

    Constraints:

    • 1 <= jewels.length, stones.length <= 50
    • jewels and stones consist of only English letters.
    • All the characters of jewels are unique.

    就是判断一个字符串里的字符有多少个出现在了另一个字符串里……没啥意思……就把jewels变成set然后遍历stones看在不在set里……

    1. class Solution {
    2. public int numJewelsInStones(String jewels, String stones) {
    3. Set<Character> set = new HashSet<>();
    4. for (char c : jewels.toCharArray()) {
    5. set.add(c);
    6. }
    7. int count = 0;
    8. for (char c : stones.toCharArray()) {
    9. if (set.contains(c)) {
    10. count++;
    11. }
    12. }
    13. return count;
    14. }
    15. }

    也可以直接用indexOf……

    1. class Solution {
    2. public int numJewelsInStones(String jewels, String stones) {
    3. int count = 0;
    4. for (char c : stones.toCharArray()) {
    5. if (jewels.indexOf(c) != -1) {
    6. count++;
    7. }
    8. }
    9. return count;
    10. }
    11. }

    还有人用regex的……Loading...

  • 相关阅读:
    江坪水利枢纽工程施工组织设计——碾压混凝土围堰
    哈工大李治军老师操作系统笔记【14】:进程同步与信号量(Learning OS Concepts By Coding Them !)
    【节能学院】浅谈中低压母线装设弧光保护的必要性
    css常用选择器用法和示例说明
    解决ODOO12 恢复数据库提示内存不够报错
    Redis的安装教程(Windows+Linux)【超详细】
    基于模型改进的LADRC在废液焚烧系统中的应用
    从零学算法377
    微信小程序-生命周期
    Linux route命令实战:route 命令实战教程,配置静态路由,删除路由表项
  • 原文地址:https://blog.csdn.net/qq_37333947/article/details/127810104
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号