close

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

 

解題:看起來是說J中的元素在S中出現的次數,使用雙層迴圈可以簡單地完成

python code:

 

class Solution:
    def numJewelsInStones(self, J: str, S: str) -> int:
        count=0
        for i in J:
            for k in S:
                if i==k:
                    count+=1
        return count

arrow
arrow
    文章標籤
    leetcode python
    全站熱搜
    創作者介紹
    創作者 低階ㄇㄋ 的頭像
    低階ㄇㄋ

    蟲匯聚之所

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()