码迷,mamicode.com
首页 > 其他好文 > 详细

Jewels and Stones

时间:2018-07-26 21:12:22      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:ide   代码   pos   different   tin   ons   repr   out   bsp   

题目描述

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 };

 

Jewels and Stones

标签:ide   代码   pos   different   tin   ons   repr   out   bsp   

原文地址:https://www.cnblogs.com/gsz-/p/9373834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!