题目描述:

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.

要完成的函数:

int numJewelsInStones(string J, string S)

说明:

1、给定两个字符串,字符串中的所有字符都代表石头种类,其中J字符串中的石头种类是玉石,S字符串中的石头种类是你已有的石头种类。要求根据J字符串判断你已有的石头中有多少是玉石,返回玉石的个数。字符大小写敏感。

2、这道题很容易,暴力解法就是双重循环,时间复杂度是O(n^2),我们也可以通过增加空间复杂度,建立哈希表,来降低时间复杂度。

代码如下:

    int numJewelsInStones(string J, string S)
{
unordered_set<char>set1(J.begin(),J.end());//转换为set存储
int i=0,s1=S.size(),count=0;
while(i<s1)
{
count+=set1.count(S[i]);//如果S[i]代表的石头是玉石,那么count+=1
i++;
}
return count;
}

上述代码实测9ms,beats 79.34% of cpp submissions。

leetcode-771-Jewels and Stones(建立哈希表,降低时间复杂度)的更多相关文章

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

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

  2. LeetCode --> 771. Jewels and Stones

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

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

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

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

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

  5. LeetCode 771 Jewels and Stones 解题报告

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

  6. 771. Jewels and Stones - LeetCode

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

  7. 【Leetcode_easy】771. Jewels and Stones

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

  8. 【Leetcode】Jewels and Stones

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

  9. [LeetCode题解]160. 相交链表 | 双指针 + 哈希表

    方法一:双指针 解题思路 假设链表存在相交时,headA 的长度为 a + c,headB 的长度为 b + c.如果把 headA 连上 headB,headB 连上 headB 的话,当遍历这两个 ...

随机推荐

  1. 服务器安装Ubuntu的那些坑

    1. 虽然简体中文很亲切,但请选择English,否则极有可能安装途中报错 2. 安装完各种系统文件后,请注意选择启动Disk,一不小心跳过了貌似只好重装 3. 进入后无法使用apt-get,总提示需 ...

  2. centos6.5 svn服务端搭建

    一.前言 Subversion是一个免费的开源的版本管理系统,它是作为CVS(Concurrent Versions System)的取代品出现的.本文简单介绍了Subversion在centos上的 ...

  3. oracle 创建一个用户,只能访问指定的对象

    1>创建一个ORACLE 的用户 create user username identified by pws; 2>给用户授权  grant connect,resource to us ...

  4. sqlserver三种数据集合运算

    2.1   并集运算(UNION) (1)UNION ALL(不删除重复行) Code: 1 SELECT empID,empName,position,degree 2 FROM Employees ...

  5. python学习——查找计算机中文件位置

    有时想查找某个文件时,却忘记了文件在计算机中存放的位置,这是一个经常遇到的问题. 当然如果你使用windows 7的话,可以直接用右上角的搜索框来搜索. 最近在学习python,正好拿这个来练练手,写 ...

  6. code2039 骑马修栏杆

    欧拉通路: find_circuit(结点i){ 当结点i有邻居时 选择任意一个邻居j: 删除边(i,j)或者做访问标记: find_circuit(结点j): 输出或存储节点i: } 本题注意点的编 ...

  7. 4.4.3 Java中的指针:Unsafe类

    java多线程编程的无锁CAS底层都是通过 Unsafe进行操作的:源码如下 public final boolean compareAndSet(int expect, int update) { ...

  8. springboot+swagger集成

    一.swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容 ...

  9. ceph中pool的管理

    1.创建pool 创建ceph pool的命令如下,它的参数包括pool名字.PG和PGP的数量. 若少于5个OSD, 设置pg_num为128. 5~10个OSD,设置pg_num为512. 10~ ...

  10. hadoop理解

    Hadoop的主核心有2部分: 1,HDFS 2, MapReduce 首先: HDFS HDFS(Hadoop Distributed File System,Hadoop分布式文件系统),它是一个 ...