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:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

Input: J = "z", S = "ZZ"
Output: 0

代码

解法1:

class Solution {
public:
int numJewelsInStones(string J, string S) {
int nCount = ; for(int i = ; i < J.length(); i++) {
for(int j = ; j < S.length(); j++) {
if(J[i] == S[j]) nCount++;
}
} return nCount;
}
};

解法2:

class Solution {
public:
int numJewelsInStones(string J, string S) {
int res = ;
set<char> setJ(J.begin(), J.end());
for (char s : S) if (setJ.count(s)) res++;
return res;
}
};

地址: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. python︱字符操作杂记(split、zip...)

    字符串特别是中文在python里面还是有很多需要注意的地方. . . . 一.字符串 . . 1.字符串连接 方式一:单个字符相连用 + 就可以: 方式二:list组内相连,可以用join: 方式三: ...

  2. g++基本用法

    用法:g++[选项]文件... g++编译流程: main.cxx #include <iostream> using namespace std; int main(void) { co ...

  3. class-朴素贝叶斯NaiveBayes

    1 朴素贝叶斯法的学习与分类1.1 基本原理2 参数估计2.1 极大似然估计2.2 算法2.3 贝叶斯估计 1 朴素贝叶斯法的学习与分类 Naive Bayes是基于贝叶斯定理和特征条件独立的假设的分 ...

  4. RobotFramework下的http接口自动化Set Request Body 关键字的使用

    Set Request Body关键字用来设置http 请求时的body 信息,尤其是在post 请求时,经常需要用到这个关键字. 该关键字接收一个参数,[ body ] 示例1:登录博客园(http ...

  5. Keras官方中文文档:序贯模型

    快速开始序贯(Sequential)模型 序贯模型是多个网络层的线性堆叠,也就是"一条路走到黑". 可以通过向Sequential模型传递一个layer的list来构造该模型: f ...

  6. 【BZOJ2037】Sue的小球(动态规划)

    [BZOJ2037]Sue的小球(动态规划) 题面 BZOJ 题解 莫名想到这道题目 很明显是一样的 设\(f[i][j][0/1]\)表示已经接到了\(i-j\)这一段的小球 当前在\(i\)或者在 ...

  7. [SCOI2016]美味

    按位从高往低贪心,枚举到第i位,只需要判断这2^i长度的区间是否有菜,用主席树就可以了 # include <bits/stdc++.h> # define RG register # d ...

  8. java支付宝开发-01-沙箱环境接入

    一.沙箱环境说明 (1)蚂蚁沙箱环境(Beta)是协助开发者进行接口功能开发及主要功能联调的辅助环境.沙箱环境模拟了开放平台部分产品的主要功能和主要逻辑(当前沙箱支持产品请参考“沙箱支持产品列表”). ...

  9. Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入

    一.目标 实现在Linux下用pycharm调试工具/Python开发 Linux使用vi/vim工具写Python,缺点:调试不方便,无代码提示.跳转等诸多功能. Windows使用idle/pyc ...

  10. ZFS建池建卷和格式化

    建池 zpool create pool_name path -f  (例如path=/dev/sdb) zfs set primarycache=metadata pool_name (关闭数据缓存 ...