771:宝石与石头
This commit is contained in:
parent
41ae96c425
commit
1d4e333fb8
58
src/main/java/leetcode/editor/cn/JewelsAndStones.java
Normal file
58
src/main/java/leetcode/editor/cn/JewelsAndStones.java
Normal 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)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<p> 给定字符串<code>J</code> 代表石头中宝石的类型,和字符串 <code>S</code>代表你拥有的石头。 <code>S</code> 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。</p>
|
||||||
|
|
||||||
|
<p><code>J</code> 中的字母不重复,<code>J</code> 和 <code>S</code>中的所有字符都是字母。字母区分大小写,因此<code>"a"</code>和<code>"A"</code>是不同类型的石头。</p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> J = "aA", S = "aAAbbbb"
|
||||||
|
<strong>输出:</strong> 3
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> J = "z", S = "ZZ"
|
||||||
|
<strong>输出:</strong> 0
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>注意:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>S</code> 和 <code>J</code> 最多含有50个字母。</li>
|
||||||
|
<li> <code>J</code> 中的字符不重复。</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 657</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user