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

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

时间: 2024-11-08 07:18:46

Jewels and Stones的相关文章

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

771. 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 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

Leetcode #771. 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

[LeetCode&Python] Problem 771: 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

771. Jewels and Stones - Easy

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

letCode(771 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 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

[Swift]LeetCode771. 宝石与石头 | 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 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

leetCode题解之Jewels and Stones

1.题目描述 2.分析 使用HashTable 是解决这种的好方法. 3.代码 1 int numJewelsInStones(string J, string S) { 2 map<char,int> m; 3 for(char c : S) 4 m[c]++; 5 6 int ans = 0; 7 for( char c: J) 8 ans += m[c]; 9 return ans; 10 11 } 原文地址:https://www.cnblogs.com/wangxiaoyong/p/

【博客搬家旧文】leetcode 771. Jewels and Stones

今天开通了博客园 ,之前的博客就不用了.之后再陆陆续续把之前的博文重新放到这里来.有标题这个tag的都是搬运的旧博客的文章.希望在这里是个新的开始,嘻嘻. import java.util.Scanner; class Solution { public static int numJewelsInStones(String J, String S) { int count=0; for(int i=0; i<J.length(); i++){ for(int j=0; j<S.length(