首页 > 其他分享 >[leeccode]771. Jewels and Stones

[leeccode]771. Jewels and Stones

时间:2023-03-23 14:31:55浏览次数:48  
标签:Stones stones 771 jewels int Character Jewels result jc


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 to know how many of the stones you have are also jewels.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

  • The characters in 

J

solution:

public int numJewelsInStones(String j, String s) {
        int result = 0;
        Set<Character> jc = new HashSet<Character>();
        for (Character i : j.toCharArray()) {
            jc.add(i);
        }

        for (Character i : s.toCharArray()) {
            if (jc.contains(i)) {
                result++;
            }
        }
        return result;
    }




标签:Stones,stones,771,jewels,int,Character,Jewels,result,jc
From: https://blog.51cto.com/u_6813689/6145012

相关文章

  • 研究生本科毕业论文参考文献格式(GB/T 7714-2005)-Endnote
    Endnote可以很方便地在写论文的过程中进行文献引用。目前很多毕业论文引用的格式要求一般都是GB/T7714-2005。Endnote有自带的ChineseStdGBT7714(Author-Year)或Ch......
  • P7712 [Ynoi2077] hlcpq 题解
    虚式优化建图题。首先有一个很显然的暴力,对于两条相交的线段连一条边,然后跑割点。这个暴力的问题在于边与点的时间复杂度相差过大,无论是空间还是时间都无法承受,所以可以......
  • 770~771 EL概述,运算符
    El表达式1.概念:ExpressionLanguage表达式语言ExpressionLanguage是JSTL1.0为方便存取数据所自定义的语言。2.作用:替换和简化jsp页面中java代码的......
  • 【ABC270D】Stones
    首先很显然直接贪心是不行的,就好像背包的时候一直选价值最大的肯定会假一样。诶?背包?这题还真有点像背包。考虑像背包一样设,\(f_i\)表示剩下\(i\)个石子的先手最大获得......
  • LeetCode - 771. Jewels and Stones
    题目You’regivenstrings​​J​​​representingthetypesofstonesthatarejewels,and​​S​​representingthestonesyouhave.EachcharacterinSisa......
  • D. Bear and Company (cf771D)
    D.BearandCompany(cf771D)tag:dp题目链接题意:给你一串长度为n的字符串,(2<=n<=75),字母全为大写字母,你可以通过一次操作交换任意一对相邻字母。字符串合法当且仅当......
  • CF1771C 质数分解+思维技巧题 *1600 (普及+/提高)
    Problem-1771C-Codeforces有 T 组数据,每组数据给出 n 和长度为 n的数列 a[i]​,判断有没有两个数不互质,如果有输出"YES",没有输出"NO"n≤2e51≤a[i]≤1e9难......
  • [LeetCode] 1753. Maximum Score From Removing Stones
    Youareplayingasolitairegamewith threepiles ofstonesofsizes a​​​​​​, b,​​​​​​and c​​​​​​respectively.Eachturnyouchoosetw......
  • P7710 [Ynoi2077] stdmxeypz 题解
    对@LHFdalao的题解进行一些补充说明。因为他讲的实在是太难懂了。最后在@_•́へ•́╬_dalao的帮助下才勉强知道怎么做。不过他的代码非常简洁易懂,有需求的可以去看......
  • CF1771C
    Idea注意到取值范围不大,因此可以打出质数的表,然后筛选用map存起来进行判断。Code#include<bits/stdc++.h>#defineF(i,a,b)for(inti=a;i<=b;i++)#defineFd(i,......