1.原题:

https://leetcode.com/problems/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".

意译翻译:J是一个string,它代表的是那些被认为是你想要的的钻石种类。比如J=ad,那说明a和d这两种钻石可以被视为你想要的珠宝。

而S也是一个string,它代表的是你所有的钻石。比如S = aAfdd,那说明一共有4种不同的钻石(注意这里区分大小写),而d这种钻石有两个。

你需要写一个程序来得知你想要的这种几种钻石一共有几个存在在这个S里面。

2.解题思路:

这道题的思路非常多。因为这道题理论上讲,想要做出来基本上很无脑,直接写一个for loop扫描就完事了,但是那样的话代码很慢还吃内存。

因此大多数人的重点是关注如何降低时间和空间复杂度上。

建议去看看讨论区,里面有无数种思路,这里只选择我个人来说喜欢的方法。

a.需要的知识点:

为了最小化时间复杂度,leetcode大佬使用了unorder set(这个数据结构基于哈希表),这种数据结构就是方便查阅,增添和删除,但是因为无序所以更占用内存,不过考虑到我们这次的目的不需要排序,所以没问题。

参考阅读:http://www.cplusplus.com/reference/unordered_set/unordered_set/

b.解题思路:

class Solution {
public:
int numJewelsInStones(string J, string S) {
int res = 0;
unordered_set<char> setJ(J.begin(), J.end());
for (char s : S)
if (setJ.count(s))

res++;
return res;
}
};

核心就是unorder set.我们创建一个unorder set来储存J的char(因为J主要作用是查询)

然后用char s 去一个个代表S里面的种类的for loop来对比J里面的种类,如果找到就加进res里面,最后输出,其实就是要找到最合适的数据结构。

leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法

    1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...

  2. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  3. leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)

    Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...

  4. leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping

    1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...

  5. leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST

    1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...

  6. leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero

    1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...

  7. leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点

    1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...

  8. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

  9. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

随机推荐

  1. PHP的常用字符串处理

    一.拼接字符串 拼接字符串是最常用到的字符串操作之一,在PHP中支持三种方式对字符串进行拼接操作,分别是圆点.分隔符{}操作,还有圆点等号.=来进行操作,圆点等号可以把一个比较长的字符串分解为几行进行 ...

  2. js消除小游戏(极简版)

    js小游戏极简版 (1) 基础布局 <div class = "box"> <p></p> <div class="div&qu ...

  3. Apache ServiceComb 开源两周年,聊聊其与微服务的前世今生

    欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...

  4. 使用正则表达式实现(加减乘除)计算器(C#实现)

    起因:公司领导要求做一款基于行业规范的计算器, 然后需要用户输入一些数据,然后根据用户输入的数据满足某些条件后,再根据用户输入的条件二进行加减乘除运算.;-) 期间因为查找规范等形成数据表的某一列是带 ...

  5. 洛谷 题解 P5535 【【XR-3】小道消息】

    我又双叒叕被包菜辣! P5535 [XR-3]小道消息(这道题是个大水题 在题干中这位良心的作者就提醒了我们: 你可能需要用到的定理--伯特兰-切比雪夫定理. 那么什么是伯特兰-切比雪夫定理? 我也不 ...

  6. 大数据之Linux用户权限设置

    用户 是Linux系统工作中重要的一环, 用户管理包括 用户 与 组 管理,在Linux系统中, 不论是由本级或是远程登录系统, 每个系统都必须拥有一个账号, 并且对于不同的系统资源拥有不同的使用权限 ...

  7. [TimLinux] openpyxl 操作Excel

    from openpyxl import Workbook from openpyxl.styles import Color, PatternFill, Font from openpyxl.sty ...

  8. B. Magic Stick

    题目:魔法棒 题意:可以对一个正数进行变换,如果数字是偶数,那么它可以变成3 * a / 2 如果这个数大于1,那么它可以变成a - 1 有两个数x和y,询问是否可以通过这些操作从x变成y,输出YES ...

  9. ARTS-S C语言多线程传参数

    #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h& ...

  10. tensorflow处理mnist(二)

    用卷积神经网络解决mnist的分类问题. 简单的例子 一行一行解释这个代码. 这个不是google官方的例子,但是很简洁,便于入门.tensorflow是先定义模型,最后赋值,计算.为了讨论问题方便, ...