这道题是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(宝石与石头)的更多相关文章

  1. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  2. [LeetCode] Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  3. Leetcode771.Jewels and Stones宝石与石头

    给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...

  4. [LeetCode] 771. Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  5. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  6. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  7. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  8. Java实现 LeetCode 771 宝石与石头(这是真暴力)

    771. 宝石与石头 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 ...

  9. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

随机推荐

  1. SVN中如何去除版本控制器

    SVN,大家都熟悉,做项目都知道,不知道你有没有遇到每次提交的代码的时候,都会把bin和obj自动生成的文件夹提交SVN服务器上 其实这里都不需要提交,每次生成都提交,可能还会容易冲突,如何不让bin ...

  2. 解析Javascript事件冒泡机制(转)

    本文转自:http://blog.csdn.net/luanlouis/article/details/23927347 1. 事件 在浏览器客户端应用平台,基本生都是以事件驱动的,即某个事件发生,然 ...

  3. vue cli 脚手架上多页面开发 支持webpack2.x

    A yuri demo for webpack2 vue multiple page.我看到有一些项目多页面项目是基于webapck1.0的,我这个是在webpack2.x上布置修改.  项目地址:  ...

  4. 自定义orgmode中加粗字体的颜色

    自定义orgmode中加粗字体的颜色 Table of Contents 1. orgmode中加粗字体的默认处理 2. 设置设置加粗字体的颜色 1 orgmode中加粗字体的默认处理 在orgmod ...

  5. spring boot图片上传至远程服务器并返回新的图片路径

    界面上传图片时考虑到可能会有用户的图片名称一致,使用UUID来对图片名称进行重新生成. //UUIDUtils public class UUIDUtils { public static Strin ...

  6. VC和MATLAB混合开发需要注意的一个问题

    作者:朱金灿 来源:http://blog.csdn.net/clever101 如果你的操作系统是64位操作系统,那么直接运行MATLAB的安装文件下的Setup.exe会默认安装的是64位的MAT ...

  7. iOS之创建CocoaPods公有库教程

    简介 在开发过程中,经常会使用到第三框架,我们通过一个pod install命令,很方便的就将第三方框架加到我们自己的项目中. 如果我们也想将自己写的组件或库开源出去,让别人也可以通过pod inst ...

  8. thinkphp写的登录注册的小demo

    和asp.net类似,一个FormAction对应Form文件夹 demo结构: ‘ 对于项目结构有疑问的: http://www.thinkphp.cn/document/60.html login ...

  9. c语言中的->代表什么意思

    c语言中 ->符号是什么意思? 比如c=a->b a为结构体或联合体的指针,->表示调用其成员

  10. 异步 BeginInvoke

    委托的异步调用异步多线程的三大特点:1.同步方法卡界面,原因是主线程被占用:异步方法不卡界面,原因是计算交给了别的线程,主线程空闲2.同步方法慢,原因是只有一个线程计算:异步方法快,原因是多个线程同事 ...