(找了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的更多相关文章

  1. 【LeetCode】771. Jewels and Stones 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...

  2. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

  3. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  4. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. vue项目持久化存储数据的实现代码

    方式一.使用localStorage在数据存储 1.要在浏览器刷新的时候重新存储起来 if (window.localStorage.getItem(authToken)) { store.commi ...

  2. 网易云易盾CTO朱浩齐:我们是如何用AI赋能内容安全?

    本文由  网易云发布. 5月19日,LiveVideoStack携手网易云易盾,共同打造了“娱乐多媒体开发应用实践”专题,帮助开发者和泛娱乐平台运营人员,提升技术能力,突破难点,拓展思路与视野. 在专 ...

  3. 使用VS2015 编译 64位的boost库

    别人写的编译参考: 目标:使用VS2015 编译 64位的boost库. 一直以来都是在Win32环境下Build和使用boost,但现在基本上每天都在64位Win7下工作,所以很有必要把这几天的经验 ...

  4. Linux入门进阶第六天——登录文件、开机与模块管理

    一.登录文件概述 1.什么是登录文件 简单的说,就是记录系统活动信息的几个文件, 例如:何时.何地(来源 IP).何人 (什么服务名称).做了什么动作 (讯息登录啰). 换句话说就是:记录系统在什么时 ...

  5. 20155338 2016-2017-2 《Java程序设计》第10周学习总结

    20155338 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 · 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事 ...

  6. 考研编程练习----Kruskal

    #include <stdio.h> #include <stdlib.h>   #define MAX 100   /* 定义边(x,y),权为w */ typedef st ...

  7. c++ 结构指针和双向链表

    结构指针 为结构指针动态分配内存 结构中的结构 双向链表 结构指针 struct mytime { //char name[256]; int hour;//时 int min; //分 i ...

  8. 安装centos minimal 版本后的网络配置(linux)

    1.修改网卡配置文件 2.重启网络服务 3.测试网络

  9. Java 注解 初探 (一)

    自JDK1.5之后,就开始出现注解.想要了解注解的来源和注解的用法,通过搜索引擎大都是针对某一个注解的解释,很难找到关于注解系列的文章,便自己看下. 基于Annotation的注释,说明Annotai ...

  10. python全栈开发-面向对象-初识2

    python_17_day 今日主要内容: 1.类空间,对象空间,查询顺序. 2.组合. 1.类空间,对象空间,查询顺序. class Person: animal = '高级动物' soul = ' ...