版权声明:本文为博主原创文章,欢迎转载!转载请保留原博客地址。 https://blog.csdn.net/grllery/article/details/85767244
771. 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 in S
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" Output: 3
Example 2:
Input: J = "z", S = "ZZ" Output: 0
Note:
S
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
题目:计算字符串S中有多少个字符是在字符串J中出现过的.注意:字符串J中的字符均是不同的.
Solution1.对字符串J建立一个map或者set,然后依次判断S中的字符是否在J中.
Solution2:因为S和J中的字符仅有大小写的英文字符,可利用ASCII码表,创建一个大小为128的数组char_num,先统计S中的每个字符出现的次数,然后对J中的每个字符,在数组char_num中查找其对应的值.
#include<unordered_map>
#include<iostream>
using namespace std;
class Solution {
public:
int numJewelsInStones(string J, string S) {
unordered_map<char, int> char_num;
int res = 0;
for(const auto &c : J){
char_num[c] = 1;
}
//判断Stone中的每个元素是否在Jewels中
for(const auto &c: S){
auto iter = char_num.find(c);
if(iter != char_num.end())
res++;
}
return res;
}
};
class Solution2 {
public:
int numJewelsInStones(string J, string S) {
int res = 0;
//只有大小写字母,对应的ASCII码A:65,a:97.
int char_num[128] = {0};
for(const auto &c : S){
char_num[c]++;
}
//对于Jewels中的每个元素,在Stone对应的char_num数组中查找
for(const auto &c: J){
res += char_num[c];
}
return res;
}
};
int main(int argc, char const *argv[])
{
Solution sln;
cout << sln.numJewelsInStones("aA", "aAAbbbb") << endl;
return 0;
}