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. 实验:将系统进程映射移到 Python 字典中

    参考官方文档,测试下列代码,把oracle的进程映射到python的字典中: [oracle@ycr python]$ more pro_get.py import reimport subproce ...

  2. March 29 2017 Week 13 Wednesday

    It's during our darkest moments that we must focus to see the light. 在最黑暗的时刻,最该努力看到光. I always thoug ...

  3. March 13 2017 Week 11 Monday

    A warm smile is the universal language of kindness. 温暖的笑容是善意的通用语. Face comes from the heart. Just sm ...

  4. March 6 2017 Week 10 Monday

    A well-spent day brings happy sleep. 丰盈白日,安眠晚间. Recently my sleep is not so good, for one thing I go ...

  5. C++中临时对象的产生与优化

    看到了几篇讲的不错的博客,这里收集起来 不明白的地方互相参考 https://blog.csdn.net/fangqingan_java/article/details/9320769 https:/ ...

  6. python 用cookie模拟登陆网站

    import re import requests def get_info(url): headers = { "Cookie" :"***************** ...

  7. ACM-百度之星资格赛之Energy Conversion——hdu4823

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/lx417147512/article/details/26400079 Energy Convers ...

  8. sparkStreamming原理

    一.Spark Streamming 是基于spark流式处理引擎,基本原理是将实时输入的数据以时间片(秒级)为单位进行拆分,然后经过spark引擎以类似批处理的方式处理每个时间片数据. 二.Spar ...

  9. POJ 2365【YY水题】

    题目链接:POJ 2365 Rope Rope Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7488   Accepted ...

  10. div可编辑框,去除粘贴文字样式😄

    上个月做了个聊天的需求(网页版的).说到聊天都想到输入框,说到输入框都会想到input,但是input标签是不支持插入图片的(包括areatext标签).查阅了一些资料就看到div标签有一个属性con ...