之前在leetcode刷了一些题,放到这里做个记录以及分享。
//leetcode url:https://leetcode.com/problems/jewels-and-stones/description/
class Solution {
public:
int numJewelsInStones(string J, string S) {
vector jv;
vector sv;
for(int i=0;i<J.length();i++){
jv.push_back(J[i]);
}
for(int i=0;i<S.length();i++){
sv.push_back(S[i]);
}
sort(jv.begin(),jv.end());
sort(sv.begin(),sv.end());
int num=0;
int checkIndex=0;
for(int i=0;i<jv.size();i++){ if(checkIndex>=sv.size())
break;
if(jv[i]<sv[checkIndex])
continue;
for(;checkIndex<sv.size();checkIndex++){
if(jv[i]==sv[checkIndex]){
num++;
continue;
}
if(jv[i]<sv[checkIndex])
break;
}
}
return num;
}
};