算法-哈希表-宝石与石头

在这里插入图片描述

class Solution {
    
    
    public int numJewelsInStones(String jewels, String stones) {
    
    
        int ans = 0;
        int jewelsLength = jewels.length();
        int stonesLength = stones.length();

        Set<Character> set = new HashSet<>();
        for(int i = 0; i < jewelsLength; i++) {
    
    
            set.add(jewels.charAt(i));
        }

        for(int i = 0; i < stonesLength; i++) {
    
    
            if(set.contains(stones.charAt(i))) {
    
    
                ans++;
            }
        }

        return ans;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45100361/article/details/113061547