From 1d4e333fb8de495f191a61fe2333301fab12e116 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 3 Sep 2021 13:03:04 +0800 Subject: [PATCH] =?UTF-8?q?771:=E5=AE=9D=E7=9F=B3=E4=B8=8E=E7=9F=B3?= =?UTF-8?q?=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/JewelsAndStones.java | 58 +++++++++++++++++++ .../editor/cn/doc/content/JewelsAndStones.md | 23 ++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/JewelsAndStones.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/JewelsAndStones.md diff --git a/src/main/java/leetcode/editor/cn/JewelsAndStones.java b/src/main/java/leetcode/editor/cn/JewelsAndStones.java new file mode 100644 index 0000000..60971b3 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/JewelsAndStones.java @@ -0,0 +1,58 @@ +//给定字符串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 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) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/JewelsAndStones.md b/src/main/java/leetcode/editor/cn/doc/content/JewelsAndStones.md new file mode 100644 index 0000000..da66379 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/JewelsAndStones.md @@ -0,0 +1,23 @@ +

 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

+ +

J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a""A"是不同类型的石头。

+ +

示例 1:

+ +
输入: J = "aA", S = "aAAbbbb"
+输出: 3
+
+ +

示例 2:

+ +
输入: J = "z", S = "ZZ"
+输出: 0
+
+ +

注意:

+ + +
Related Topics
  • 哈希表
  • 字符串

  • 👍 657
  • 👎 0
  • \ No newline at end of file