public static int numJewelsInStones(String J, String S) {
int result = 0;
for (int i = 0; i < S.length(); i++) {
for (int j = 0; j < J.length(); j++) {
if (S.charAt(i) == J.charAt(j)) {
result++;
}
}
}
return result;
}
- 遍历字符串S
- 在S的每一项中遍历字符串J
- 如果S和J的字符串相同,result就加1
- 返回result