—Easy
https://leetcode.com/problems/jewels-and-stones/
Code:
class Solution: def numJewelsInStones(self, J, S): ans = 0 for elt in S : if elt in J: ans += 1 return ans # s = Solution() # s.numJewelsInStones(J = "z", S = "ZZ") # s.numJewelsInStones(J = "aA", S = "aAAbbbb")
思路:
1.暴力遍历就ok,因为J和S都不会很长,所以虽然算法复杂度很大,O(j*s),但是所用时间不会很多,另外一种是做标记的hash思想,复杂度应该是O(j+s)
2.记得是返回值!不需要之前的print!