zoukankan      html  css  js  c++  java
  • [LeetCode] 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 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.

    使用set来存在J中不同字符的。

    遍历S寻找出现在set中的字符并统计。

    class Solution {
    public:
        int numJewelsInStones(string J, string S) {
            unordered_set<char> s(J.begin(), J.end());
            int res = 0;
            for (auto& ch : S) {
                if (s.count(ch) > 0)
                    res++;
            }
            return res;
        }
    };
    // 9 ms
  • 相关阅读:
    【18焦作网络赛 J】 大数开方
    最小圆覆盖
    高斯消元
    回文自动机 PAM
    后缀自动机 SAM
    后缀数组 SA
    左偏树(可并堆)
    动态树LCT(Link-Cut-Tree)
    职场自我推销10大金点子
    数据库笔记
  • 原文地址:https://www.cnblogs.com/immjc/p/8377625.html
Copyright © 2011-2022 走看看