lintcode-1038. 珠宝和石头

  • 题目描述

给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝.

J中的字母一定不同,JS中的字符都是字母。 字母区分大小写,因此"a""A"是不同的类型.

  • 算法思路

将J,S处理成list,再用两层for循环即可解决。

  • code

class Solution:
    """
    @param J: the types of stones that are jewels
    @param S: representing the stones you have
    @return: how many of the stones you have are also jewels
    """
    def numJewelsInStones(self, J, S):
        # Write your code here
        list_J = list(J)
        list_S = list(S)
        list_Z = []
        for i in list_J:
            for j in list_S:
                if j == i:
                    list_Z.append(j)
        n = len(list_Z)
        return n

  

原文地址:https://www.cnblogs.com/yeshengCqupt/p/9864739.html