LeetCode——宝石与石头

题目描述:J表示的是各种宝石,S表示持有的石头,判断出S中到底有多少个J中的元素,注意是区分大小写的

class Solution {
    public int numJewelsInStones(String J, String S) {
        int res = 0;
        Set set = new HashSet();
        for(char j:J.toCharArray())
            set.add(j);
        for(char s:S.toCharArray())
            if(set.contains(s))
                res++;
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/wo8vqj68/article/details/82928622