(找了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. linux-2.6.22.6内核启动分析之编译体验

    1 解压缩.打补丁操作 1.1 打开ubuntu,通过FTP将windows相应文件夹下的linux-2.6.22.6.tar.bz2和补丁文件linux-2.6.22.6-jz2440.patch上 ...

  2. java->php之在线子域名查询-接口光速版

    因为不懂java,所以 用php重写了大佬的 在线子域名查询-接口光速版 http://sbd.ximcx.cn/  这是大佬的 然后 改一下 ,用php 其实就是改了几行代码而已,jquery和aj ...

  3. 双端队列 ADT接口 数组实现

    Deque ADT接口 DEQUEUE.h: #include <stdlib.h> #include "Item.h" void DEQUEUEinit(int); ...

  4. DP_最长公共子序列/动规入门

    学自:https://open.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html 最长公共子序列:(本文先谈如何求出最长公共子序列的长度,求出最长公 ...

  5. 如何将M文件转成独立可执行程序

    如何将MATLAB程序编译成独立可执行的程序?生成独立可执行的程序(exe文件)步骤    1.安装编译器.可有多种选择,matlab自带了一个LCC,推荐使用VC++6.0,我基于VS 2013实现 ...

  6. 汇编程序返回dos

    汇编程序返回dos有两种方式: 1. push ds    sub ax,ax    push ax    ...    ret 作用:一开始ds是指向psp的,在psp:0000处放着int 20h ...

  7. 20155204 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    实验一 Java开发环境的熟悉 一.实验内容及步骤 1.使用JDK编译.运行简单的java程序 步骤一:在linux界面下运行终端 步骤二:在终端中打开待编译文件的文件夹 步骤三:使用 javac 文 ...

  8. 将linux上的Java代码上传到码云

    将linux上的Java代码上传到码云 1.在linux上直接输入命令获取git sudo apt-get install git 显示资源被占用,按照图中方法强制安装 2.建立与教材配套的目录结构 ...

  9. 20145207 《Java 程序设计》实验三 (敏捷开发与XP实践)实验报告

    <Java 程序设计>实验三 (敏捷开发与XP实践)实验报告 目录 改变 敏捷开发与XP实践实验要求 实验成果 课后思考 改变 修改了之前仅仅是贴了图片,连代码都没粘的状态.增加了自己的思 ...

  10. 【LOJ6433】【PKUSC2018】最大前缀和

    [LOJ6433][PKUSC2018]最大前缀和 题面 题目描述 小 C 是一个算法竞赛爱好者,有一天小 C 遇到了一个非常难的问题:求一个序列的最大子段和. 但是小 C 并不会做这个题,于是小 C ...