LeetCode771-宝石与石头 LeetCode771-宝石与石头暴力(O(mn))枚举所有的字符进行比较 使用集合(O(m+n))1234567891011public int numJewelsInStones(String jewels, String stones) { HashSet<Character> set = new HashSet<>(); int res = 0; for (int i = 0; i < jewels.length(); i++) { set.add(jewels.charAt(i)); } for (int i = 0; i < stones.length(); i++) { if (set.contains(stones.charAt(i))) res++; } return res; } LeetCode 力扣 计算机基础 字符串 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处! LeetCode58-最后一个单词的长度 Previous LeetCode709-转换成小写字母 Next Please enable JavaScript to view the comments