174.Jewels and Stones
题目
You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”.
Example 1:
Input: J = “aA”, S = “aAAbbbb”
Output: 3
Example 2:
Input: J = “z”, S = “ZZ”
Output: 0
Note:
S and J will consist of letters and have length at most 50.
The characters in J are distinct.
思路
问题抽象为:找S中包含有几个J中的字符;
1.取出J中所有不同的字符全部放到set中;
2.遍历S中的字符,如果在map1中则num++;
3.return num
代码
java
public int numJewelsInStones(String J, String S) {int num = 0;Set<Character> set = new HashSet<Character>(); for (int i = 0; i< J.length(); i++) {set.add(J.charAt(i));}for (int i = 0; i< S.length(); i++) {if (set.contains(S.charAt(i))) {num++;}}return num;}
python
def numJewelsInStones(self, J, S):""":type J: str:type S: str:rtype: int"""#定义一个set,存放J中不同的字符s1 = set()num = 0for j in J : s1.add(j)for s in S :if s in s1 :num = num + 1return num
C++
int numJewelsInStones(string J, string S) {int num = 0;std::set<char> s1;for (int i = 0; i < J.size(); i++) {s1.insert(J[i]);}for (int i = 0; i< S.size(); i++) {if (s1.find(S[i]) != s1.end()) {num++;}}return num;}
174.Jewels and Stones相关推荐
- LeetCode - 771. Jewels and Stones
题目 You're given strings J representing the types of stones that are jewels, and S representing the s ...
- 1. Jewels and Stones (宝石与石头)
Jewels and Stones leetcode 771 题目 You're given strings J representing the types of stones that are j ...
- C#LeetCode刷题之#771-宝石与石头(Jewels and Stones)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3812 访问. 给定字符串J 代表石头中宝石的类型,和字符串 S代 ...
- 1038. Jewels And Stones
描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因此& ...
- 06 Jewels and Stones
关注 每天一道编程题 专栏,一起学习进步. 题目 You're given strings J representing the types of stones that are jewels, an ...
- Jewels and Stones(宝石和石头)
1 问题抛出 You're given strings J representing the types of stones that are jewels, and S representing t ...
- Jewels and Stones
转载于:https://www.cnblogs.com/Julietma/p/10096867.html
- 【博客搬家旧文】leetcode 771. Jewels and Stones
今天开通了博客园 ,之前的博客就不用了.之后再陆陆续续把之前的博文重新放到这里来.有标题这个tag的都是搬运的旧博客的文章.希望在这里是个新的开始,嘻嘻. import java.util.Scann ...
- leetCode题解之Jewels and Stones
1.题目描述 2.分析 使用HashTable 是解决这种的好方法. 3.代码 1 int numJewelsInStones(string J, string S) { 2 map<char, ...
最新文章
- 高频数据交换下Flutter与ReactNative的对比
- NLP高阶:一文走遍完整自然语言处理流程
- 计算机课代表自荐信,课代表自荐信范文
- 记一次EF Core连接MySql、Oracle
- PHP云尚发卡,云尚发卡1.5.7添加(极客支付)
- 1块钱,能买来财富自由吗?
- 如何自动搜出更好、更小、更快的NLP模型?
- caffe的googlenet模型使用
- uniapp uView 微信小程序弹框二维码canvas不显示,以及显示后跟随滑动的问题
- 妄想性仮想人格障害 汉化补丁(BUG修正)
- 使用scrapy框架爬取链家网站租房信息
- maskrcnn-benchmark安装过程
- CollapsingToolbarLayout 标题字体及颜色设置
- VMware安装虚拟机
- Resistors in Parallel(Gym - 102028E 2018 ICPC 焦作E题 大数+规律C++版)
- CRC校验,用于大家参考和日后查阅,内容引用github,非本人创作。
- Biotin-SNA,EBL;生物素化黑接骨木凝集素(SNA,EBL)
- 淘宝引力魔方复制后怎么移除?有什么优势?
- 再次聚焦收入分配制度改革
- 打折策略模式java_策略模式(java)