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. 69道Spring面试题及答案

    目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...

  2. Prometheus PromQL 基础

    目录 时序 4 种类型 Counter Gauge Histogram Summary Histogram vs Summary 操作符 时序 4 种类型 Prometheus 时序数据分为 Coun ...

  3. java String比较,“==”和“equal”区别

    public static void main(String[] args){ String str1 = new String("str"); String str2 = new ...

  4. windows虚拟机中DNS服务配置

    在linux虚拟机中进行DNS服务配置并进行正向解析反向解析我博客中已经写过,下面 我来介绍一下在windows虚拟机中DNS服务的配置使用. 1.打开一台windows虚拟机中服务器管理器——角色— ...

  5. RestSharp Simple REST and HTTP API Client for .NET

    var client = new RestClient("http://example.com"); // client.Authenticator = new HttpBasic ...

  6. 华为云MVP:来自工业制造领域的微服务与云平台实践

    [摘要] 首先,和大家先聊聊的是为什么微服务.DevOps和云计算会在各个产业大行其道;其次,再谈谈微服务架构设计有那些自己独特的设计思想,和传统的SOA有什么区别;最后,我们再一起看一看在工业领域云 ...

  7. 爬虫学习(二)--爬取360应用市场app信息

    欢迎加入python学习交流群 667279387 爬虫学习 爬虫学习(一)-爬取电影天堂下载链接 爬虫学习(二)–爬取360应用市场app信息 代码环境:windows10, python 3.5 ...

  8. Springboot实现登录功能

    SpringBoot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再 ...

  9. Keras开发一个神经网络

    关于Keras:Keras是一个高级神经网络API,用Python编写,能够在TensorFlow,CNTK或Theano之上运行. 使用一下命令安装: pip install keras 在Kera ...

  10. java的传参究竟是按值传递的还是按引用传递的

    这里来弄清楚Java的传参究竟是按值传递的还是按引用传递的. 形参和实参 传参的概念里,有形参和实参的区分.形参是定义方法名和方法体的时候使用的参数,目的是用来接收调用该方法的时候传入的参数:实参是调 ...