这道题是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. [转]AngularJS移动开发中的坑汇总

    使用AngualrJs开发移动App已经快半年了,逐渐积累了很多AngularJS的问题,特别是对于用惯了Jquery的开发者,转到AngularJS还是需要克服很多问题的.不像Jquery那样侧重D ...

  2. TLS、SSL、HTTPS以及证书

    转自:http://www.cnblogs.com/kyrios/p/tls-and-certificates.html 最近在研究基于ssl的传输加密,涉及到了key和证书相关的话题,走了不少弯路, ...

  3. java 设计模式 之 桥梁模式

    桥梁模式:将抽象和实现解耦,使两者可以独立的变化.解释:将两个有组合关系,强耦合的对象,各自抽象然后解耦.(类关系图看https://www.cnblogs.com/blogxiao/p/951388 ...

  4. 异步 BeginInvoke

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

  5. mac ssd开启trim模式

    开启方法 sudo trimforce enable

  6. Java-NestedClass(Interface).

    内部类(Nested Class) 内部类:即在一个类中还包含着另外一个类,一般是作为匿名类或者是使用数据隐藏时使用的.例子: //内部类 class Out{ private int age = 1 ...

  7. html输入框去除记忆功能

    自动完成功能,只需把AUTOCOMPLETE设为off即可,如: 整个表单禁止自动完成 HTML code <FORM method=post action="submit.asp&q ...

  8. Codeforces Round #273 (Div. 2)-B. Random Teams

    http://codeforces.com/contest/478/problem/B B. Random Teams time limit per test 1 second memory limi ...

  9. 使用gcc -g编译,gdb调试时仍然存在“no debug symbols found”的错误

    今天为调试一段代码,使用gcc将程序用-g选项重新编译.但是使用gdb进行debug时,仍然出现“no debug symbols found”的错误.仔细检查了一下Makefile,原来后面定义的连 ...

  10. shell脚本,配置文件加载顺序,以及什么时候加载。

    在linux系统中,有/etc/profile,/etc/bashrc ,~/.bash_profile,~/bashrc这四个配置文件,这些文件,会自动的在某些时候加载,也就是点一下,一般都是些别名 ...