今天开始每天把每日一题写了。这道题为字符串处理。题目给定字符串J和字符串S,其中字符串J说明为宝石的字符类型,S说明我们手中所有的石头,要求求出手中宝石的数量。string处理方式与set比较相似,先尝试一下像set一样来处理:
class Solution {
public:
int numJewelsInStones(string J, string S) {
int Jlength = J.length();
int Slength = S.length();
if(Slength == 0) return 0;
if(Jlength == 0) return 0;
int count = 0;
for(int i = 0; i < Jlength; ++i)
{
for(int j = 0; j < Slength; ++j)
{
if(J[i] == S[j]) count++;
}
}
return count;
}
};
一遍过,不错。