58 lines
1.5 KiB
Java
58 lines
1.5 KiB
Java
//给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
|
||
//
|
||
// J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
|
||
//
|
||
// 示例 1:
|
||
//
|
||
// 输入: J = "aA", S = "aAAbbbb"
|
||
//输出: 3
|
||
//
|
||
//
|
||
// 示例 2:
|
||
//
|
||
// 输入: J = "z", S = "ZZ"
|
||
//输出: 0
|
||
//
|
||
//
|
||
// 注意:
|
||
//
|
||
//
|
||
// S 和 J 最多含有50个字母。
|
||
// J 中的字符不重复。
|
||
//
|
||
// Related Topics 哈希表 字符串 👍 657 👎 0
|
||
|
||
package leetcode.editor.cn;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.Collections;
|
||
import java.util.List;
|
||
|
||
//771:宝石与石头
|
||
class JewelsAndStones {
|
||
public static void main(String[] args) {
|
||
//测试代码
|
||
Solution solution = new JewelsAndStones().new Solution();
|
||
}
|
||
|
||
//力扣代码
|
||
//leetcode submit region begin(Prohibit modification and deletion)
|
||
class Solution {
|
||
public int numJewelsInStones(String jewels, String stones) {
|
||
int count = 0;
|
||
List<Character> list = new ArrayList<>();
|
||
for (char ch : jewels.toCharArray()) {
|
||
list.add(ch);
|
||
}
|
||
for (char ch : stones.toCharArray()) {
|
||
if (list.contains(ch)) {
|
||
count++;
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
}
|
||
//leetcode submit region end(Prohibit modification and deletion)
|
||
|
||
} |