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

要完成的函数:

int numJewelsInStones(string J, string S)

说明:

1、给定两个字符串,字符串中的所有字符都代表石头种类,其中J字符串中的石头种类是玉石,S字符串中的石头种类是你已有的石头种类。要求根据J字符串判断你已有的石头中有多少是玉石,返回玉石的个数。字符大小写敏感。

2、这道题很容易,暴力解法就是双重循环,时间复杂度是O(n^2),我们也可以通过增加空间复杂度,建立哈希表,来降低时间复杂度。

代码如下:

    int numJewelsInStones(string J, string S)
    {
        unordered_set<char>set1(J.begin(),J.end());//转换为set存储
        int i=0,s1=S.size(),count=0;
        while(i<s1)
        {
            count+=set1.count(S[i]);//如果S[i]代表的石头是玉石,那么count+=1
            i++;
        }
        return count;
    }

上述代码实测9ms,beats 79.34% of cpp submissions。

原文地址:https://www.cnblogs.com/king-3/p/9042849.html

时间: 2024-10-18 10:07:04

leetcode-771-Jewels and Stones(建立哈希表,降低时间复杂度)的相关文章

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

顺序查找,折半查找,二叉排序树的建立,哈希表的建立

以下四个验证性实验都做. (1)顺序查找验证 (2)折半查找验证 (3)二叉排序树的建立 (4)哈希表的建立 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<string.h> #include<algorithm> using namespace std; class dijiuzhang { public: int a[1

[LeetCode&amp;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 珠宝和石头

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

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

[leetcode 40. 组合总和 II] 不排序使用哈希表+dfs递归 vs 排序栈+回溯

题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. 说明: 所有数字(包括目标数)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] 示例 2: 输入

[转]:Delphi 中的哈希表(1): THashedStringList

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Send