leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
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珠宝和钻石的更多相关文章
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- 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 ...
- 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 ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
随机推荐
- sudo控制用户对系统命令的使用权限
sudo控制用户对系统命令的使用权限 sudo相关概念 普通用户涉及到超级权限的运用,管理员如果想让该普通用户通过su来切换到root获得超级权限,就必须把root权限密码告诉用户.但是如果普通用户有 ...
- js基础-函数-var和let的区别
javaScript简介 javaScript历史 1995年,Netscape公司是凭借Navigator浏览器成为当时第一代互联网公司. 网景公司希望在HTML界面上加一点动态效果,于是叫Bren ...
- 简单理解http协议的特性
http协议一种数据传输的规范,像我们的在发送数据的时候,我们无法保证发送与接收的类型是一致的,它就保证了我们传输的同一个类型数据. 特性: 1.灵活:我们不管传输什么数据,图片,文件,文字,都可以进 ...
- luogu P2672 推销员 |贪心
题目描述 阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有N家住户,第ii家住户到入口的距离为Si米.由于同一栋 ...
- 大数据之Linux基础
回顾这一个多月以来闭关学大数据的一些相关重要知识,就当复习,顺便以备以后查看 Linux学习第一步自然是安装Linux. 关于Linux 首先介绍下Linux,Linux系统很多程序员开发者其实都耳熟 ...
- Undefined symbols for architecture x86_64"_OBJC_CLASS_$_QQApiInterface 怎么搞
今天上午报了一个这样的错误 解决办法 如此如此 ~~ 然后编译 看看报的什么错误 还是不行的话就重新导入三方库 添加依赖库 结果build success
- bug小结
在不同的文件下面可以创建同一个包,但是不能创建同一个class文件!!! ParameterType:需要写实体类的类型,最好不要写实体的别名 这是因为我们在配置mybatis的配置文件时已经说明 ...
- [TimLinux] CSS 纯CSS实现动画展开/收起功能
内容转自CSS世界,理解之后进行了简化,简化后代码: <!DOCTYPE html> <html> <head> <meta charset=utf-8 /& ...
- BZOJ1002 [FJOI2007]轮状病毒(最小生成树计数)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7125 Solved: 3878[Submit][Status][Discuss] Descripti ...
- CodeForces1006F-Xor-Paths
F. Xor-Paths time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...