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 的话,当遍历这两个 ...
随机推荐
- ubuntu Qt5 librealsense opencv
08:11:47: 进程"/usr/bin/make"正常退出. 08:11:47: 配置没有改变, 跳过 qmake 步骤. 08:11:47: 正在启动 "/usr/ ...
- [Token] 从index.jsp中获取Token
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def holde ...
- SDN网络工具
TcpDump 根据使用者的定义对网络上的数据包进行截获的包分析工具. http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html ...
- TF Boys (TensorFlow Boys ) 养成记(一):TensorFlow 基本操作
本资料是在Ubuntu14.0.4版本下进行,用来进行图像处理,所以只介绍关于图像处理部分的内容,并且默认TensorFlow已经配置好,如果没有配置好,请参考官方文档配置安装,推荐用pip安装.关于 ...
- springMVC框架集成tiles模板
将tiles模板集成到springMVC框架下,大概流程如下: 1.在配置文件中加入tiles支持 我的servlet配置文件名为spring-mvc.xml.具体配置如下: <?xml ver ...
- 编写高质量代码改善C#程序的157个建议——建议69:应使用finally避免资源泄漏
建议69:应使用finally避免资源泄漏 除非发生让应用程序中断的异常,否则finally总是会先于return执行.finally的这个语言特性决定了资源释放的最佳位置就是在finally块中:另 ...
- 【图解HTTP】第一章 了解web及网络基础
[图解HTTP]了解Web及网络基础 Web页面是如何呈现的?根据Web浏览器地址栏中指定的URL,Web浏览器从Web服务器端获取文件资源(resource)等信息,从而显示出Web页面. 这种通过 ...
- 配置hive环境以及mysql配置后必须做
1.先在主节点上安装阿里云配置(看别的文档) 2.把需要的两个jar包加入进来(放到hadoop用户目录下面即可即/home/hadoop/) mysql-connector-java-5.1.47. ...
- vim command
问题:对多行缩进 简述:向左缩是<,向右缩是> 详述:在命令状态,按v进入visual状态,选中多行,再按>,相当于一个TAB键:再选中多行,按<试试 ------------ ...
- hdu 4284 Travel(壮压DP&TSP&floyd)
Travel Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...