1. 程式人生 > >[Leetcode從零開刷]771. Jewels and Stones

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

翻譯:

簡單講就是,給你一個代表不同寶石的字串,和一個代表石頭的字串,從石頭字串裡找出屬於寶石的石頭,並返回個數。
注意,區分大小寫即a和A不一樣。

解答:

1.通過遍歷字串S和J,兩兩進行比較,判斷stone中有多少顆jewel。

  • 時間複雜度為O(s * j)。(s為字串S的長度,j為字串J的長度)。
  • 空間複雜度為O(1)。

java:

class Solution {
    public int numJewelsInStones(String J, String S) {
        int res = 0;
        Set set = new HashSet();
        for(char c:J.toCharArray())set.add(c);
        for(char s:S.toCharArray())if(set.contains(s))res++; 
        return
res; } }
class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int res = 0;
        set<char> setJ(J.begin(), J.end());
        for (char s : S) if (setJ.count(s)) res++;
        return res;       
    }
};

c++版本:

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int ret =0;
        set<char>set;
        for(int i = 0;i<J.length();i++)
            set.insert(J[i]);
        for(int i = 0;i<S.length();i++)
            if(set.count(S[i]))
                ret++;

        return ret;
    }
};

相關推薦

[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

[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

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

  今天開通了部落格園 ,之前的部落格就不用了。之後再陸陸續續把之前的博文重新放到這裡來。有標題這個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

leetcode771.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 寶石與石頭

解法一: 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】Same Tree

沒錯我就是伍聲2009的粉絲,從今天起,模仿《從零單排》系列,菜雞單刷LeetCode! 題目: Given two binary trees, write a function to check if they are equal or not.  Two binary

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】Find the Duplicate Number

題目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive)

[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

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

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寶石與石頭(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