leetcode-771-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:
SandJwill consist of letters and have length at most 50.- The characters in
Jare 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(建立哈希表,降低时间复杂度)的更多相关文章
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- [LeetCode] 771. Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- LeetCode 771 Jewels and Stones 解题报告
题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- [LeetCode题解]160. 相交链表 | 双指针 + 哈希表
方法一:双指针 解题思路 假设链表存在相交时,headA 的长度为 a + c,headB 的长度为 b + c.如果把 headA 连上 headB,headB 连上 headB 的话,当遍历这两个 ...
随机推荐
- kalman处理realsense数据
代码来自:https://www.cnblogs.com/zjuhjm/archive/2012/12/29/2838472.html import numpy as npimport matplot ...
- 28-组合数(dfs)
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 组合数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- JVM垃圾回收算法及回收器详解
引言 本文主要讲述JVM中几种常见的垃圾回收算法和相关的垃圾回收器,以及常见的和GC相关的性能调优参数. GC Roots 我们先来了解一下在Java中是如何判断一个对象的生死的,有些语言比如Pyth ...
- python移除系统多余大文件-乾颐堂
文件多了乱放, 突然有一天发现硬盘空间不够了, 于是写了个python脚本搜索所有大于10MB的文件,看看这些大文件有没有重复的副本,如果有,全部列出,以便手工删除 使用方式 加一个指定目录的参数 比 ...
- 第一次用FontLad~
FontLad 是一款制作字体的工具 使用流程: 1. 下载.安装软件FontLab Studio2. File->New菜单,创建一个工程文件,会自动创建一个ASCII码表对应的字体表3. ...
- sklearn中的随机森林
阅读了Python的sklearn包中随机森林的代码实现,做了一些笔记. sklearn中的随机森林是基于RandomForestClassifier类实现的,它的原型是 class RandomFo ...
- ngix 创建新的网站
1. 进入ngix 的目录的conf 目录 的 vhosts 2. 复制一份新的v2.edc.com.conf 3. server_name : v2.edc.com root : /ali/... ...
- javascript总结38: 神奇的this
1 this的特性 this 是在函数中的 this 的指向 是在函数调用的时候决定的 this的指向. 谁调用这个函数,函数中的this就指向谁 function fn (){ console.lo ...
- 编写高质量代码改善C#程序的157个建议——建议89:在并行方法体中谨慎使用锁
建议89:在并行方法体中谨慎使用锁 除了建议88所提到的场合,要谨慎使用并行的情况还包括:某些本身就需要同步运行的场合,或者需要较长时间锁定共享资源的场合. 在对整型数据进行同步操作时,可以使用静态类 ...
- Linux route命令 显示getnameinfo failed [UNKNOWN]
Redhat配置IPv6以后,执行route显示getnameinfo failed [UNKNOWN] 修改/etc/hosts,增加主机名后,重启服务器,问题解决