LeetCode771. 宝石与石头

思路:
本题考查了哈希表,说白了就是2个字符串a和b,为b中有多少字母出现在a中。把a中的字母通过哈希表记录下来,然后遍历b计数即可


代码:
python3

class Solution:
    def numJewelsInStones(self, J: str, S: str) -> int:
        rec={}
        for i in J:
            rec[i]=1
        ret=0
        for i in S:
            ret+=rec.get(i,0)
        return ret

cpp

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        set record;
        for(int i=0;i

你可能感兴趣的:(LeetCode771. 宝石与石头)