【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 短连接、长连接、轮询、长轮询、WebSocket
短连接 建立连接——数据传输——关闭连接...建立连接——数据传输——关闭连接 定义:短连接是指通讯双方有数据交互时,就建立一个连接,数据发送完成后,则断开此连接,即每次连接只完成一项业务的发送. 应 ...
- 如何通过审计安全事件日志检测密码喷洒(Password Spraying)攻击
许多渗透测试人员和攻击者通常都会使用一种被称为“密码喷洒(Password Spraying)”的技术来进行测试和攻击.对密码进行喷洒式的攻击,这个叫法很形象,因为它属于自动化密码猜测的一种.这种针对 ...
- springboot 使用@ConfigurationProperties注入配置属性
导入依赖,让springboot支持@ConfigurationProperties 注解 <!-- 支持 @ConfigurationProperties 注解 --> <depe ...
- 20155203 2016-2017-2 《Java程序设计》第2周学习总结
20155203 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 第三章的主要内容是使用Java语言设计程序的基础语法的学习 java语言中的计算:如果两个数 ...
- 20155322 2016-2017-2 《Java程序设计》第4周学习总结
20155322 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 本周的学习内容为课本第六章与第七章: 第六章主要讲继承和多态.首先是我们为什么要学习继承和多 ...
- 20155325 加分作业 实现pwd
要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 准备 思路 问题 1.如何获取当前目录的节点号 Linux ...
- 20155330 2016-2017-2 《Java程序设计》第九周学习总结
20155330 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 学习目标 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛 ...
- 用 Qt 的 QAudioOutput 类播放 WAV 音频文件
用 Qt 的 QAudioOutput 类播放 WAV 音频文件 最近有一个项目,需要同时控制 4 个声卡播放不同的声音,声音文件很简单就是没有任何压缩的 wav 文件. 如果只是播放 wav 文件, ...
- Ajax在Django中的应用
一.什么是Ajax AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传 ...
- WPF binding Tag
使用一个控件控制另外一个控件的显示与隐藏.