【LeetCode】Jewels and Stones(宝石与石头)
这道题是LeetCode里的第771道题。
题目要求:
给定字符串
J代表石头中宝石的类型,和字符串S代表你拥有的石头。S中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J中的字母不重复,J和S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。示例 1:
输入: J = "aA", S = "aAAbbbb"
输出: 3
示例 2:
输入: J = "z", S = "ZZ"
输出: 0
注意:
S和J最多含有50个字母。J中的字符不重复。
送送送送送送送送送送分题!!!
提交代码:
class Solution {
public:
int numJewelsInStones(string J, string S) {
int hashmap[128]{0},sum=0;
for(int i=0;i<J.length();i++)
if(hashmap[J[i]]==0)hashmap[J[i]]=1;
for(int i=0;i<S.length();i++){
if(hashmap[S[i]])sum++;
}
return sum;
}
};
运行结果:

个人总结:
当时截图没有保存,现在实例多了,时间跟不上了。这个题目的简单程度堪比于 Hello World!多贴一些代码吧:
public:
int numJewelsInStones(string J, string S) {
int n[52]={0};
int count=0;
if(J.empty()||S.empty())
return 0;
for(auto tmp:J){
if(tmp<='Z'&&tmp>='A'){
++n[tmp-'A'+26];
}
else
++n[tmp-'a'];
}
for(auto tmp:S){
if(tmp<='Z'&&tmp>='A'){
if(n[tmp-'A'+26]!=0)
++count;
}
else{
if(n[tmp-'a']!=0)
++count;
}
}
return count;
}
};
↑↑↑曾经的最优解 4ms,现在也不行了。↑↑↑
//java 1
class Solution {
public int numJewelsInStones(String J, String S) {
int num = 0;
for(int i=0;i<S.length();i++){
if(J.contains(S.charAt(i)+"")){
num++;
}
}
return num;
}
}
//java 2
class Solution {
public int numJewelsInStones(String J, String S) {
if (J == null || S == null) {
return 0;
}
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < J.length(); i ++) {
set.add(J.charAt(i));
}
int result = 0;
for (int i = 0; i < S.length(); i ++) {
if (set.contains(S.charAt(i))) {
result ++;
}
}
return result;
}
}
//java 3
class Solution {
public int numJewelsInStones(String J, String S) {
int flag=0;
char[] js=J.toCharArray();
char[] ss=S.toCharArray();
Set<Character> set=new HashSet<>();
for(char c:js){
set.add(c);
}
for(char s:ss){
if(set.contains(s))
flag++;
}
return flag;
}
}
//java script 1
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
var res = 0
S.split('').forEach( i => {
if(J.indexOf(i) >= 0) res++
})
return res
};
//java script 2正则表达式
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
var P = new RegExp(`[${J}]`, 'g');
var R = S.match(P);
if (!R) return 0;
return R.length;
};
//java script 3
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
var sum = 0;
for (var i = 0; i < S.length; i++) {
if (J.indexOf(S[i]) != -1) {
sum++;
}
}
return sum;
};
//java script 4
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
if (!J || !S) {
return 0;
}
var jewels = J.split('');
var stones = S.split('');
var count = 0;
stones.forEach(item => {
if (jewels.indexOf(item) > -1) {
count++;
}
});
return count;
};
↑↑↑这些都是 Java 解法,可以看出解法大致相同。因为题目简单,大家都在尝试如何使用优雅的解法hhh↑↑↑
【LeetCode】Jewels and Stones(宝石与石头)的更多相关文章
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- Leetcode771.Jewels and Stones宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- [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(宝石与石头)
题目描述 给定字符串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】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- Java实现 LeetCode 771 宝石与石头(这是真暴力)
771. 宝石与石头 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
随机推荐
- 最大流bfs
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- c#操作ecxel的一些资源(downmoon搜集)
c#操作ecxel的一些资源(downmoon搜集) 工作需要,邀月收集了几个操作excel的资源. 1.如何:使用 COM Interop 创建 Excel 电子表格(C# 编程指南)http:/ ...
- CSS3在hover下的几种效果
CSS3在hover下的几种效果代码分享,CSS3在鼠标经过时的几种效果集锦 效果一:360°旋转 修改rotate(旋转度数) * { transition:All 0.4s ease-in-out ...
- CSS Secrets 翻译笔记 01: CSS coding tips
.firDemoButton{ padding: 6px 16px; border: 1px solid #446d88; background: #58a linear-gradient(#77a0 ...
- java数据结构和算法06(红黑树)
这一篇我们来看看红黑树,首先说一下我啃红黑树的一点想法,刚开始的时候比较蒙,what?这到底是什么鬼啊?还有这种操作?有好久的时间我都缓不过来,直到我玩了两把王者之后回头一看,好像有点儿意思,所以有的 ...
- POSTGRESQL 存储过程实战
转了N多的SQL语句,可是自己用时,却到处是坑啊,啊,啊!!!!!!!!!!!!!!! 想写一个获取表中最新ID值. 上代码 CREATE TABLE department( ID INT PRIMA ...
- Oracle Data Integrator 12c 安装(ODI安装)
Oracle Data Integrator 12c 安装(ODI安装) 企业版安装步骤(包含独立安装步骤) 官网下载Oracle Data Integrator 12cR2 (12.2.1.0.0) ...
- window.close() 关闭当前浏览器页
function eseFun() { var browserName = navigator.appName; //获取浏览器名称 if(browserName == "Netscape& ...
- Spring MVC能响应HTTP请求的原因?
很多Java面试官喜欢问这个问题: 一个Spring MVC的项目文件里,开发人员没有开发自己的Servlet,只通过注解@RequestMapping定义了方法home能响应发向 /mvc/test ...
- UVA116 Unidirectional TSP 单向TSP
分阶段的DAG,注意字典序的处理和路径的保存. 定义状态d[i][j]为从i,j 出发到最后一列的最小花费,转移的时候只有三种,向上,向下,或平移. #include<bits/stdc++.h ...