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 日期 题目地址 ...
随机推荐
- sudo:无法解析主机 解决方案
你如果电脑中没有vim,用gedit也可以. 打开文件以后,将其中的 127.0.1.1 xxxxx xxx 替换成你电脑的自己的名字即可.
- dojo、iframe和FusionCharts兼容性
今天,我们项目组对项目收尾检查,却突然发现了一个问题,FusionCharts出现兼容性问题. 在火狐浏览器上,项目运行正常:在IE8(标准模式下)运行正常,但是在IE8杂项出现兼容性问题.经过检查, ...
- HighCharts之2D柱状图、折线图和饼图的组合图
HighCharts之2D柱状图.折线图和饼图的组合图 1.实例源码 ColumnLinePie.html: <!DOCTYPE html> <html> <head&g ...
- AM335x(TQ335x)学习笔记——USB驱动移植
对于AM335x来讲,TI维护的USB驱动已经非常完善了,本文称之为移植,实际上仅仅是配置内核选项使能USB HOST/OTG功能.废话少说,直接动手开启AM335x的USB驱动配置项. Step1. ...
- freemarker写select组件报错总结(二)
1.错误描述 六月 25, 2014 11:32:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...
- 自用公共js文件
// 加载配置文件var instance = axios.create({ baseURL: 'http://zy-shop.tincent.me/Wechat/', headers: { 'Con ...
- Android查缺补漏(线程篇)-- AsyncTask的使用及原理详细分析
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8515304.html 一.AsyncTask的使用 AsyncTask是一种轻 ...
- freemark声明变量,boolean,date,date日期格式转换成String类型的(五)
<br/>assign用来定义变量<#assign name="刘德华"><br/> 获取assign定义变量的值:${name} <br ...
- 【BZOJ1018】堵塞的交通(线段树)
[BZOJ1018]堵塞的交通(线段树) 题面 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可 以被看成是一个2行C列的矩形网 ...
- 【洛谷1032 】【CJOJ1711】【NOIP2002】字串变换
###题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换 ...