leetCode Day 1

1 class Solution(object): 2 def numJewelsInStones(self, J, S): 3 """ 4 :type J: str 5 :type S: str 6 :rtype: int 7 """ 8 s = 0 9 for i in J: 10 for j in S: 11 if (i==j): 12 s += 1 13 return s
Runtime: 24 ms, faster than 85.32% of Python online submissions for Jewels and Stones.
Memory Usage: 10.7 MB, less than 100.00% of Python online submissions for Jewels and Stones.
Nicer way:
1 class Solution(object): 2 def numJewelsInStones(self, J, S): 3 """ 4 :type J: str 5 :type S: str 6 :rtype: int 7 """ 8 return sum([1 for c in S if c in J])
Runtime: 20 ms, faster than 100.00% of Python online submissions for Jewels and Stones.
Memory Usage: 10.7 MB, less than 100.00% of Python online submissions for Jewels and Stones.
浙公网安备 33010602011771号