题目网址:https://leetcode.com/problems/jewels-and-stones/description/
大意:就是定义一个String J,这个String里面的每个character都是一种宝石,再给你一堆石头String S,让你找到这堆石头里面的每个宝石。比较简单
class Solution:
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
i = 0
for Jitem in J:
for Sitem in S:
if Jitem == Sitem:
i += 1
return i
a = Solution()
print (a.numJewelsInStones('aA','aAAbbbb'))
其实就是两层for循环,还有更简单的写法
def numJewelsInStones2(self,J,S):
return sum(s in J for s in S)