题目

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相关推荐

  1. LeetCode - 771. Jewels and Stones

    题目 You're given strings J representing the types of stones that are jewels, and S representing the s ...

  2. 1. Jewels and Stones (宝石与石头)

    Jewels and Stones leetcode 771 题目 You're given strings J representing the types of stones that are j ...

  3. C#LeetCode刷题之#771-宝石与石头(Jewels and Stones)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3812 访问. 给定字符串J 代表石头中宝石的类型,和字符串 S代 ...

  4. 1038. Jewels And Stones

    描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因此& ...

  5. 06 Jewels and Stones

    关注 每天一道编程题 专栏,一起学习进步. 题目 You're given strings J representing the types of stones that are jewels, an ...

  6. Jewels and Stones(宝石和石头)

    1 问题抛出 You're given strings J representing the types of stones that are jewels, and S representing t ...

  7. Jewels and Stones

    转载于:https://www.cnblogs.com/Julietma/p/10096867.html

  8. 【博客搬家旧文】leetcode 771. Jewels and Stones

    今天开通了博客园 ,之前的博客就不用了.之后再陆陆续续把之前的博文重新放到这里来.有标题这个tag的都是搬运的旧博客的文章.希望在这里是个新的开始,嘻嘻. import java.util.Scann ...

  9. leetCode题解之Jewels and Stones

    1.题目描述 2.分析 使用HashTable 是解决这种的好方法. 3.代码 1 int numJewelsInStones(string J, string S) { 2 map<char, ...

最新文章

  1. 高频数据交换下Flutter与ReactNative的对比
  2. NLP高阶:一文走遍完整自然语言处理流程
  3. 计算机课代表自荐信,课代表自荐信范文
  4. 记一次EF Core连接MySql、Oracle
  5. PHP云尚发卡,云尚发卡1.5.7添加(极客支付)
  6. 1块钱,能买来财富自由吗?
  7. 如何自动搜出更好、更小、更快的NLP模型?
  8. caffe的googlenet模型使用
  9. uniapp uView 微信小程序弹框二维码canvas不显示,以及显示后跟随滑动的问题
  10. 妄想性仮想人格障害 汉化补丁(BUG修正)
  11. 使用scrapy框架爬取链家网站租房信息
  12. maskrcnn-benchmark安装过程
  13. CollapsingToolbarLayout 标题字体及颜色设置
  14. VMware安装虚拟机
  15. Resistors in Parallel(Gym - 102028E 2018 ICPC 焦作E题 大数+规律C++版)
  16. CRC校验,用于大家参考和日后查阅,内容引用github,非本人创作。
  17. Biotin-SNA,EBL;生物素化黑接骨木凝集素(SNA,EBL)
  18. 淘宝引力魔方复制后怎么移除?有什么优势?
  19. 再次聚焦收入分配制度改革
  20. 打折策略模式java_策略模式(java)

热门文章

  1. 「镁客早报」三星折叠屏手机中国区发布会临时取消;特斯拉在地库中突然自燃...
  2. lk中printf的实现
  3. 2006年世界杯——巴西队最后一次世界冠军
  4. 数据科学与大数据分析之项目4-主题建模
  5. 云原生k8s的前世今生--Docker
  6. Markdown一键转换到富文本格式
  7. 输出集合的所有子集(幂集)-C语言
  8. 运用python编写凯撒密码并实现_凯撒密码实现英文短句的加解密
  9. 在对象内部尽量直接訪问实例变量 --Effictive Objective-C 抄书
  10. SSD的王者 PCIe固态硬盘的未来在哪里