前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 771. Jewels and Stones

LeetCode 771. Jewels and Stones

作者头像
Angel_Kitty
发布2018-12-27 17:28:24
3700
发布2018-12-27 17:28:24
举报

771. Jewels and Stones

You're given strings J representing the types of stones that are jewels, and Srepresenting 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".

Example 1:

代码语言:javascript
复制
Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

代码语言:javascript
复制
Input: J = "z", S = "ZZ"
Output: 0

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

题目描述:大概意思是给定两个字符串 JS ,字符串 J 中的任意一个字符在字符串 S 中出现多少次,输出出现的次数。

题目分析:很简单,将 J 字符串中的每一个字符和 S 中的每一个字符进行匹配,如果能够匹配成功,则统计一次匹配成功。

python 代码:

代码语言:javascript
复制
class Solution(object):
    def numJewelsInStones(self, J, S):
        """
        :type J: str
        :type S: str
        :rtype: int
        """
        J_length = len(J)
        S_length = len(S)
        count = 0
        for i in range(J_length):
            for j in range(S_length):
                if J[i] == S[j]:
                    count = count + 1
                    
        return count

C++ 代码:

代码语言:javascript
复制
class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int J_length = J.length();
        int S_length = S.length();
        int count = 0;
        for(int i = 0; i < J_length; i++){
            for(int j = 0; j < S_length; j++){
                if(J[i] == S[j]){
                    count++;
                }
            }
        }
        return count;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 771. Jewels and Stones
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档