zoukankan      html  css  js  c++  java
  • Jewels and Stones

    题目描述

    You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis 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:

    Input: J = "aA", S = "aAAbbbb"
    Output: 3
    

    Example 2:

    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.

    解题思路

    此题难度为easy,遍历S中每个letter s并判断s是否在J中存在,若存在个数加一。

    代码

     1 class Solution {
     2 public:
     3     int numJewelsInStones(string J, string S) {
     4         int num = 0;
     5         for (auto s : S) {
     6             if (J.find(s) != string::npos)
     7                 num++;
     8         }
     9         return num;
    10     }
    11 };
  • 相关阅读:
    javascript 介绍
    命令提示符(命令行)
    node(1) hello world
    用Vgg16来提取特征
    VGG16学习
    注意力模型
    统计学习方法
    数字图像处理(五)
    数字图像处理(四)
    BN
  • 原文地址:https://www.cnblogs.com/gsz-/p/9373834.html
Copyright © 2011-2022 走看看