好久没做算法题了,找个网站做做练习一下,网站链接:LeetCode
这道题很简单,一个暴力就出来了
class Solution {
public int numJewelsInStones(String J, String S) {
int count =0;
int h = J.length();
for(int i=0;i<h;++i) {
for(int j=S.indexOf(J.charAt(i));j<=S.lastIndexOf(J.charAt(i));++j) {
if(j<0)
break;
if(J.charAt(i)==S.charAt(j))
++count;
}
}
return count;
}
}