LeetCode 771. Jewels and Stones
You are 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 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.
題目大意:J是珠寶,S是自己擁有的石頭,判斷自己擁有的石頭中有多少個珠寶~
分析:將J中的所有字元在hash陣列中標記為1,再遍歷S,統計hash == 1的個數即為所求~
class Solution { public: int numJewelsInStones(string J, string S) { int ans = 0, hash[256] = {0}; for (auto i : J) hash[i] = 1; for (auto i : S) if (hash[i]) ans++; return ans; } };
相關推薦
【部落格搬家舊文】leetcode 771. Jewels and Stones
今天開通了部落格園 ,之前的部落格就不用了。之後再陸陸續續把之前的博文重新放到這裡來。有標題這個tag的都是搬運的舊部落格的文章。希望在這裡是個新的開始,嘻嘻。 import java.util.Scanner; class Solution { public
【博客搬家舊文】leetcode 771. Jewels and Stones
inf nbsp 嘻嘻 用戶輸入 pan spa 重新 ring expec 今天開通了博客園 ,之前的博客就不用了。之後再陸陸續續把之前的博文重新放到這裏來。有標題這個tag的都是搬運的舊博客的文章。希望在這裏是個新的開始,嘻嘻。 import java.
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 charac
LeetCode 771 Jewels and Stones 寶石與石頭
解法一: class Solution { public: int numJewelsInStones(string J, string S) { int count=0; for(int i=0;i<J.size();++i)
leetcode 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
leetcode 771. Jewels and Stones
771. Jewels and Stones You're given strings J representing the types of stones that are jewels, and Srepresenting the stones you have. E
LeetCode 771. Jewels and Stones
You are given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is
[leetcode]-771. Jewels and Stones(C語言)
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
[LeetCode&Python] Problem 771: Jewels and Stones
leetcode diff The letter cas light sid sel nbsp You‘re given strings J representing the types of stones that are jewels, and S representi
【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.&nb
【leetcode】771.Jewels and Stones 用python實現
題目描述 You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each charact
[Leetcode從零開刷]771. Jewels and Stones
Jewels and Stones 題目 You’re given strings J representing the types of stones that are jewels, and S representing the stones
771. Jewels and Stones 珠寶和石頭
rep sid enum nes mos rom font most ret You‘re given strings J representing the types of stones that are jewels, and S representing the st
LC 771. Jewels and Stones
1.題目 You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each
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
LeetCode之Jewels and Stones(Kotlin)
問題: You’re given strings J representing the types of stones that are jewels, and S representing
771. Jewels and Stones
nta stones con bsp for span return urn har class Solution { public: int numJewelsInStones(string J, string S) { ve
【LeetCode】771(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 S
【leetcode刷題】[簡單]771寶石與石頭(jewels and stones)-java
寶石與石頭 jewels and stones題目分析解答 題目 給定字串J 代表石頭中寶石的型別,和字串 S代表你擁有的石頭。 S 中每個字元代表了一種你擁有的石頭的型別,你想知道你擁有的石頭中有多少是寶石。 J 中的字母不重複,J 和 S中的所有字元都是字母
C#LeetCode刷題之#771-寶石與石頭(Jewels and Stones)
問題 給定字串J 代表石頭中寶石的型別,和字串 S代表你擁有的石頭。 S 中每個字元代表了一種你擁有的石頭的型別,你想知道你擁有的石頭中有多少是寶石。 J 中的字母不重複,J 和 S中的所有字元都是字母。字母區分大小寫,因此"a"和"A"是不同型別的石頭。 輸入: J