771:宝石与石头

This commit is contained in:
huangge1199 2021-09-03 13:03:04 +08:00
parent 41ae96c425
commit 1d4e333fb8
2 changed files with 81 additions and 0 deletions

View File

@ -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<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)
}

View File

@ -0,0 +1,23 @@
<p>&nbsp;给定字符串<code>J</code>&nbsp;代表石头中宝石的类型,和字符串&nbsp;<code>S</code>代表你拥有的石头。&nbsp;<code>S</code>&nbsp;中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。</p>
<p><code>J</code>&nbsp;中的字母不重复,<code>J</code>&nbsp;&nbsp;<code>S</code>中的所有字符都是字母。字母区分大小写,因此<code>&quot;a&quot;</code><code>&quot;A&quot;</code>是不同类型的石头。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong> J = &quot;aA&quot;, S = &quot;aAAbbbb&quot;
<strong>输出:</strong> 3
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong> J = &quot;z&quot;, S = &quot;ZZ&quot;
<strong>输出:</strong> 0
</pre>
<p><strong>注意:</strong></p>
<ul>
<li><code>S</code>&nbsp;&nbsp;<code>J</code>&nbsp;最多含有50个字母。</li>
<li>&nbsp;<code>J</code>&nbsp;中的字符不重复。</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 657</li><li>👎 0</li></div>