771. Jewels and Stones

题目网址: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)

所有题目解题方法和答案代码地址:https://github.com/fredfeng0326/LeetCode
    原文作者:fred_33c7
    原文地址: https://www.jianshu.com/p/a5831744ab24
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞