首页 > 其他分享 >leetcode-771-easy

leetcode-771-easy

时间:2022-11-04 19:12:11浏览次数:37  
标签:stones count set 771 int jewels ++ easy leetcode

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.

思路一:存 map, 最后遍历字符串即可

public int numJewelsInStones(String jewels, String stones) {

    Set<Character> set = new HashSet<>();
    for (int i = 0; i < jewels.length(); i++) {
        set.add(jewels.charAt(i));
    }

    int count = 0;
    for (int i = 0; i < stones.length(); i++) {
        if (set.contains(stones.charAt(i))) count++;
    }

    return count;
}

标签:stones,count,set,771,int,jewels,++,easy,leetcode
From: https://www.cnblogs.com/iyiluo/p/16858847.html

相关文章

  • leetcode-1200-easy
    MinimumAbsoluteDifferenceGivenanarrayofdistinctintegersarr,findallpairsofelementswiththeminimumabsolutedifferenceofanytwoelements.Retu......
  • leetcode-1984-easy
    MinimumDifferenceBetweenHighestandLowestofKScoresYouaregivena0-indexedintegerarraynums,wherenums[i]representsthescoreoftheithstudent.......
  • EasyCVR国标GB28181协议接入下的TCP和UDP模式说明及差异
    有用户在使用我们的平台时,经常会出现对于端口的疑问,同时也不了解端口的差别。今天我们来解释说明下EasyCVR平台关于国标GB28181协议接入下的TCP和UDP模式的说明及差异。......
  • 视频融合平台EasyCVR各项数据正常,却无法用海康NVR接入是什么原因?
    EasyCVR视频融合云平台开放度高、兼容性强、可支持灵活拓展与第三方集成,目前已经成为安防市场主流的视频能力层服务平台。平台可支持多协议、多类型的设备接入,包括国标GB28......
  • 我用EasyExcel优化了公司的导出(附踩坑记录)
    背景介绍最近要改一个导出的功能,在原有的基础上,在导出一份明细数据,要求导出内容加在原有excel的第二个sheet上。考虑到数据量还比较大,干脆引入阿里的EasyExcel来做......
  • leetcode 160. 相交链表 js 实现
    给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。图示两个链表在节点 c1 开始相交: ......
  • leetcode-136. 只出现一次的数字
    题目描述给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明说明:你的算法应该具有线性时间复杂度。你可以不......
  • LeetCode刷题记录.Day5
    反转链表题目链接206.反转链表-力扣(LeetCode)classSolution{public:ListNode*reverseList(ListNode*head){ListNode*temp;ListNode*c......
  • leetcode java 杨辉三角
    简介杨辉三角是一道简单题,可以通过类似一层推下一层的方式进行计算,但是好像看过一个题解,采用的方式是组合数。本来想采用组合数,尝试了double溢出尝试了long溢出,尝试......
  • LeetCode_Stack_589. N-ary Tree Preorder Traversal N 叉树的前序遍历【栈,迭代】【简
    目录​​一,题目描述​​​​英文描述​​​​中文描述​​​​示例与说明​​​​二,解题思路​​​​三,AC代码​​​​C++​​​​Java​​​​四,解题过程​​​​第一博​......