771. Jewels and Stones

作者: fred_33c7 | 来源:发表于2018-06-20 16:44 被阅读1次

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

    相关文章

      网友评论

        本文标题:771. Jewels and Stones

        本文链接:https://www.haomeiwen.com/subject/pmyxyftx.html