题目描述
过于水,用来练基础数据结构好啦
样例
def numJewelsInStones(self, J: str, S: str) -> int:
#可以进一步用set: (O(1)的查询,是个value为0的hashmap)
# S-> jewelsSet = set(J) 然后查return sum(s in jewelsSet for s in S)
return sum(J.count(i) for i in S)
"""
res = 0
for i in S:
for j in range(len(J)):
if i == J[j]:
res += 1
return res
"""
c++暴力
懒人版直接开int
int numJewelsInStones(string J, string S) {
int res = 0;
for (char j : J)
for (char s : S)
if (j == s)
res += 1;
return res;
}
或者
int numJewelsInStones(string J, string S) {
int res = 0;
for (int i = 0; i < J.size(); i ++)
for (int j = 0; j < S.size(); j++)
if (J[i] == S[j])
res += 1;
return res;
}