LeetCode --> 771. Jewels and Stones
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" |
Example 2:
Input: J = "z", S = "ZZ" |
代码
解法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的更多相关文章
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- 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】771. Jewels and Stones
(找了leetcode上最简单的一个题来找一下存在感) You're given strings J representing the types of stones that are jewels, ...
- 【LeetCode】771. Jewels and Stones 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...
随机推荐
- FFMpeg.H264解码win开发环境搭建
开发环境: vc6.0 + sp5 + vcpp5,注意vcpp5在vc6+sp6上会安装失败. 源码: ff_264_dec_vc,可用vc进行编译调试,但编译环境限定如上. 声明:该工程是ffmp ...
- Android View绘制和显示原理简介
现在越来越多的应用开始重视流畅度方面的测试,了解Android应用程序是如何在屏幕上显示的则是基础中的基础,就让我们一起看看小小屏幕中大大的学问.这也是我下篇文章--<Android应用流畅度测 ...
- 阿里巴巴开源前端框架--Weex实践
Weex是最近很火很NB的一个技术产品,因为本篇介绍的是怎样使用Weex的最佳实践,所以就不罗里吧嗦的夸它怎么怎么好了,感兴趣的可以访问Weex HomePage,或加入旺旺群:1330170019. ...
- Error Code: 1360 - Trigger does not existQuery
1.错误描述 Query: DROP TRIGGER `t_sert_cs_approve` Error occured at:2015-04-12 13:37:32 Line no.:1 Error ...
- dijit.byId("grid") is undefined
1.错误描述 TypeError:dijit.byId(...) is undefined (68 out of range 3) 2.错误原因 var gridName = dijit ...
- freemarker.template.TemplateException:Macro has no such argument:params
1.错误描述 freemarker.template.TemplateException:Macro mainSelect has no such argument:params 2.错误原因 在宏定 ...
- python实现列表倒叙打印
def func(listNode): listNode.reverse() for i in listNode: print(i) li = [1,2,3,4,5] func(li) 利用pytho ...
- 第一个bug
话不多说自己遇到的第一个小程序bug 需要渲染渲染多重元素,这个没什么.but当你要获取这个大样式的id进行各种操作时,你需要每一个子节点都加上data-=""属性这样就很麻烦了, ...
- java将字符串转换为指定的时间格式
*String dateString = "18:31:43"; try { Date date = new SimpleDateFormat("HH:mm ...
- MySQL新建用户与授权
一.登录root用户 [root@iZm5e9gg1p5y1co0usac2cZ webapps] # mysql -uroot -p 二.新建用户 mysql> create user '; ...