【Leetcode】771. Jewels and Stones
(找了leetcode上最简单的一个题来找一下存在感)
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 Tips:J为不同的字符组成的字符串,从S中找出包含J中字符的总数。
解法一 两重for循环判断字符是否相同。19ms
public int numJewelsInStones(String J, String S) {
int num = 0;
for (int i = 0; i < S.length(); i++) {
for (int j = 0; j < J.length(); j++) {
if (S.charAt(i) == J.charAt(j)) {
num++;
}
}
}
return num;
}
解法二:
只循环S 判断J中是否包含,当前的S中的字符 39ms
public int numJewelsInStones2(String J, String S) {
int num = 0;
for (int i = 0; i < S.length(); i++) {
char c = S.charAt(i);
if (J.indexOf(c) != -1) {
System.out.println(c);
num++;
}
}
return num;
}
但是啊,反而是两层for循环快一些呢!
【Leetcode】771. Jewels and Stones的更多相关文章
- 【LeetCode】771. Jewels and Stones 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- docker 容器模式下部署mysql 主从复制
1.计划用两台host来部署,分别部署一台 mysql,一主一从,2.配置好主从mysql配置文件,更改文件名即可[client]port = 3306socket = /var/run/mysqld ...
- 数据结构与算法-图的最短路径Dijkstra
一 无向图单源最短路径,Dijkstra算法 计算源点a到图中其他节点的最短距离,是一种贪心算法.利用局部最优,求解全局最优解. 设立一个visited访问和dist距离数组,在初始化后每一次收集一 ...
- Go 跨域请求问题
在使用go语言写测试服务的时候遇到了前端跨域请求问题,只需在go中加入 w.Header().Set("Access-Control-Allow-Origin", "*& ...
- Swift3.0字符串大小写转化
Swift3.0语言教程字符串大小写转化,在字符串中,字符串的格式是很重要的,例如首字母大写,全部大写以及全部小写等.当字符串中字符很多时,通过人为一个一个的转换是很费时的.在NSString中提供了 ...
- Qt QStringLiteral
zz 解释QStringLiteral 原文发表于woboq网站 QStringLiteral explained 转载 原作者: Olivier Goffart 译者:zzjin QStringL ...
- Nginx入门篇(五)之LNMP环境应用
一.LNMP组合工作原理 (1)用户通过浏览器输入域名请求Nginx web服务: (2)Nginx对请求的资源进行判断,如果是静态资源,则由Nginx返回给用户:如果是动态请求(.php文件),那么 ...
- xgboost算法教程(两种使用方法)
标签: xgboost 作者:炼己者 ------ 欢迎大家访问我的简书以及我的博客 本博客所有内容以学习.研究和分享为主,如需转载,请联系本人,标明作者和出处,并且是非商业用途,谢谢! ------ ...
- halcon中关于文本的创建以及写入
原文链接:http://blog.sina.com.cn/s/blog_61cc743001017nxr.html#FileName 1.open_file( : : FileName, FileTy ...
- MySql面试题(持续更新)
1. 左连接,右连接,内连接的概念. 左连接:以左表为主,保留左表的所有数据,并且依次拿每行数据去匹配右表所有行,如果没匹配的,右边表的数据为null. 右连接:以右表为主,保留右表的所有数据,并且依 ...
- HP VC模块Server Profile配置快速参考(With SUS)
以管理员身份登录VCM 准备进行Server Profiles的配置 在左侧导航栏中找到并点击"Server Profiles",在右侧主窗口的左下角点击"Add&quo ...