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 inS 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:

  1. Input: J = "aA", S = "aAAbbbb"
  2. Output: 3

Example 2:

  1. Input: J = "z", S = "ZZ"
  2. Output: 0

代码

解法1:

  1. class Solution {
  2. public:
  3. int numJewelsInStones(string J, string S) {
  4. int nCount = ;
  5.  
  6. for(int i = ; i < J.length(); i++) {
  7. for(int j = ; j < S.length(); j++) {
  8. if(J[i] == S[j]) nCount++;
  9. }
  10. }
  11.  
  12. return nCount;
  13. }
  14. };

解法2:

  1. class Solution {
  2. public:
  3. int numJewelsInStones(string J, string S) {
  4. int res = ;
  5. set<char> setJ(J.begin(), J.end());
  6. for (char s : S) if (setJ.count(s)) res++;
  7. return res;
  8. }
  9. };

地址:https://leetcode.com/problems/jewels-and-stones/description/

LeetCode --> 771. Jewels and Stones的更多相关文章

  1. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  2. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  3. [LeetCode] 771. Jewels and Stones 珠宝和石头

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

  4. LeetCode 771 Jewels and Stones 解题报告

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

  5. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  6. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

  7. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  8. 【Leetcode】771. Jewels and Stones

    (找了leetcode上最简单的一个题来找一下存在感) You're given strings J representing the types of stones that are jewels, ...

  9. 【LeetCode】771. Jewels and Stones 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...

随机推荐

  1. linux命令--ldconfig和ldd用法

    一.ldconfig ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig. ldconfig 命令的用途,主要是在默认搜寻目录(/ ...

  2. Struts2实现文件上传报错(一)

    1.具体报错如下 2014-5-1 23:02:38 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service( ...

  3. Ubuntu14.04下安装Flash Player

    Ubuntu14.04下安装Flash Player youhaidong@youhaidong:~$ sudo apt-get install flashplugin-nonfree [sudo] ...

  4. The Eclipse executable launcher was unable to locate its companion launcher jar的解决方法

    大家都知道eclipse是免安装的,基本上解压就能用,但是笔者在解压后打开eclipse时遇到一个奇葩的问题,如题目所示. 在网上搜了半天,基本上都说的是:The Eclipse executable ...

  5. C#图解教程 第十九章 LINQ

    LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from-let-where片段 from子句let子句w ...

  6. Bootstrap下拉菜单的使用(附源码文件)--Bootstrap

    1.Bootstrap下拉菜单的使用,源代码如下:(如有不当之处,还望大佬们指出哈……) <!DOCTYPE html> <html lang="en"> ...

  7. 动态地添加HTML控件-JavaScript基础

    相关: document对象的createElement()方法可以创建一个新的HTML控件(document.createElement("input");) setAttrib ...

  8. python socket知识点

    ---恢复内容开始--- 数据在网络上进行传播,需要通过一种通信协议.常见的通信协议有:HTTP,SMTP,DNS,FTP,SSH,SNMP,ICMP PING,DNCP. OSI(Open Syst ...

  9. C++ RCSP智能指针简单实现与应用

    智能指针的实现代码来源博客:<http://blog.csdn.net/to_be_better/article/details/53570910> 修改:添加 get()函数,用以获得原 ...

  10. VS中使用.NET Reactor进行代码混淆

    .NET Reactor相信大家都不陌生,网上使用教程也很多.但绝大多数都只介绍到软件的使用,而对于在VS中使用介绍的不多. 首先,在.NET Reactor的Help中Add In,如下图. 重启V ...