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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
toCharArray(无参)可以用来打散字符串,很好用
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
hashset可以不用指定存储类型, 配合count就能统计数量了
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
- class Solution {
- public int numJewelsInStones(String J, String S) {
- //cc
- if (J.length() == 0 || S.length() == 0) {
- return 0;
- }
- //ini set, res
- Set set = new HashSet();
- int res = 0;
- //for loop,count
- for (char j : J.toCharArray()) {
- set.add(j);
- }
- for (char s : S.toCharArray()) {
- if (set.contains(s)) res++;
- }
- //return res
- return res;
- }
- }
771. Jewels and Stones珠宝数组和石头数组中的字母对应的更多相关文章
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- [LeetCode] 771. Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- LeetCode 771. Jewels and Stones (宝石与石头)
题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...
- 【LeetCode】771. Jewels and Stones 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...
- letCode(771 Jewels and Stones )
问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...
随机推荐
- Maven下载 || 配置本地仓库 || IntelliJ IDEA配置Maven教程
本文章主要介绍1.Maven下载 2.配置本地仓库Repository 3.IDEA配置Maven 三点. 相关博客: Eclipse配置Maven https://www.cnblogs.c ...
- SVN管理多个项目版本库 (windows,linux 通用)
SVN管理多个项目版本库: . 安装SVN服务器软件,路径: C:\Program Files\Subversion . 在D盘创建svn根目录D:\SVN-CM . 在D:\SVN-CM下创建SVN ...
- windows下matplotlib的安装
在上一篇中我想用matplotlib,无奈一直装不上,就在卸了又装装了又卸,反反复复之后,终于装好了. 初学python,首先就装了numpy,倒也没有多复杂,有需要的朋友可以直接http://sou ...
- 20179203 《Linux内核原理与分析》第十一周作业
一.环境的使用和搭建 首先我的攻击机和靶机都搭建在虚拟机上,选用的是VMware Workstation Pro虚拟机. 攻击机选用的是Linux kali 2017.2版本,而靶机安装的是XP sp ...
- Js中的prototype的用法二
用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初学者莫衷一是,只知道函数都会有一个prototype属性,可以为其添加函数供实例访问,其它的就不清楚了, ...
- [转]HTMl中Meta标签详解以及meta property=og标签含义
meta是用来在HTML文档中模拟HTTP协议的响应头报文.META标签是HTML语言HEAD区的一个辅助性标签,它位于HTML文档头部的<HEAD>标记和<TITLE>标记之 ...
- java中List、Map、Set、Collection、Stack、Queue等的使用
java中这几个东西是比较常用的,虽然我用的不多,也正是因为用的不多,所以我一直搞不清楚他们之间的具体用法以及相互之间的关系,现在特单独作为一个东西来总结一下. 本文参考一下资料: 1.<jav ...
- .NET Framework、C#、CLR和Visual Studo之间的版本关系
.NET Framework.C#.CLR和Visual Studo之间的版本关系 参考 .NET Framework.C#.CLR和Visual Studo之间的版本关系
- compile cef2526
fetch --nohooks chromium cd /path/to/chromium/src# git checkout -b 51.0.2704.103 refs/tags/51.0.2704 ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...