Jewels and Stones

Description

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

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

Discuss

比较简单的题目,直接遍历就可以了。

Code

class Solution {
public int numJewelsInStones(String J, String S) {
int count = 0;
for (int i = 0; i < S.length(); i++) {
char aa = S.charAt(i);
for (int j = 0; j < J.length(); j++) {
char bb = J.charAt(j);
if (aa == bb) { count++; }
}
}
return count;
}
}

【Leetcode】Jewels and Stones的更多相关文章

  1. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  2. 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...

  3. 【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  4. 【leetcode】1033. Moving Stones Until Consecutive

    题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...

  5. 【leetcode】947. Most Stones Removed with Same Row or Column

    题目如下: On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may h ...

  6. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

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

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

  8. 【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 ...

  9. 53. Maximum Subarray【leetcode】

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

随机推荐

  1. 学习笔记-java 集合

    背景: 看的是<java核心技术 第8版>,覆盖jdk1.6.主要是对集合全局和细节进行全面掌握,较深入的理解集合.本人对java比较熟悉,但是对于细节的理解不深,知识点还不全,这是知识的 ...

  2. Struts2的学习-属性驱动和模型驱动

    1.新建空项目,并托管到码云 2.设立maven 3.设置pom.xml 4.建包 5.在java包里建立两个类 package com.nf.entity; public class User { ...

  3. leetcode 62、Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. 设计模式——原型模式(Prototype Pattern)

    原型模式:用原型实例制定创建对象的种类,并且通过拷贝这些原型创建新的对象. UML 图: 原型类: package com.cnblog.clarck; /** * 原型类 * * @author c ...

  5. 再次拿起live writer

    再次拿起live writer 第一次接触这哥们还是使用NPOI时,当时这个开源项目的发起人tonyqus让我知道了这家伙,感觉很不错,我们不需要直接用网页进行编辑内容,而且可以突破网页编辑器单调的设 ...

  6. csu 1965

    Description You are a boss who has N employers. One day, you have a message and you want to tell it ...

  7. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

    任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  8. html基本代码书写

    html的书写: datagrid---->toolbar---->按钮---->handler:function() var rows=$("gridId"). ...

  9. Git命令篇

    前文: Git有三种状态,你的文件可能处于其中之一:已提交(committed),已修改(modiffied)和已暂存(staged) 三个工作区域概念:Git仓库.工作目录以及暂存区 Git保存信息 ...

  10. 用c#语言编写1000以内能整除3的数字

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...