LeetCode 771. Jewels and Stones

题意:给出一个没有重复字母的字符串 J 表示宝石的种类,S表示拥有的石头的种类,问拥有的石头中有多少是宝石。

solution:hash记录J中宝石的种类,再去查询S中的石头是否在hash表中出现过。

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        unordered_map<char, bool> hash; // bool?
        int count = 0;
        for ( auto n : J ) {
            hash[n] = 1;
        }
        for ( auto n : S ) {
            if ( hash[n] == 1 ) {
                count++;
            }
        }
        return count;
    }
};

submission:很神奇的遇上了提交数量不足的一道题,因此没有直方图。



猜你喜欢

转载自blog.csdn.net/jack_ricky/article/details/79360589