问题描述:

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

解决方案:

  1 数组

    var numJewelsInStones = function(J, S) {
    var arr1=J.split("");
    var arr2=S.split("");
    var count=0;
    for(var i=0;i<arr2.length;i++){
    for(var j=0;j<arr1.length;j++){
    if(arr2[i]==arr1[j]){
    count++
    }
    }
    }
    return count
    };

2 字符串方法

    

    var numJewelsInStones = function(J, S) {
    var count=0;
    for(var i=0;i<S.length;i++){
    if(J.indexOf(S.charAt(i))!==-1){
    count++
    }
    }
    return count
    };

letCode(771 Jewels and Stones )的更多相关文章

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

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

  2. 【Leetcode_easy】771. Jewels and Stones

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

  3. 771. Jewels and Stones - LeetCode

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

  4. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  5. 771. Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  6. LeetCode 771 Jewels and Stones 解题报告

    题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...

  7. [LeetCode&Python] Problem 771: Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  8. 【Leetcode】771. Jewels and Stones

    (找了leetcode上最简单的一个题来找一下存在感) You're given strings J representing the types of stones that are jewels, ...

  9. 771. Jewels and Stones珠宝数组和石头数组中的字母对应

    [抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...

随机推荐

  1. C#各个版本中的新增特性详解【转】

    序言 自从2000年初期发布以来,c#编程语言不断的得到改进,使我们能够更加清晰的编写代码,也更加容易维护我们的代码,增强的功能已经从1.0搞到啦7.0甚至7.1,每一次改过都伴随着.NET Fram ...

  2. spark thrift server configuration

    # MainApplicationProperties # --master yarn --deploy-mode client 下的配置, client 模式表示,driver 是在本地机器上跑的, ...

  3. js小知识colspan和rowspan

    colspan和rowspan这两个属性用于合并表格的列或者行. colspan是"column  span"(跨列)的缩写,所以colspan属性用在td标签中,用来跨列合并单元 ...

  4. JavaScript学习——JS对象和全局函数

    1. Array对象 数组的特点:长度可变!数组的长度=最大角标+1 2.Boolean对象 如果value 不写,那么默认创建的结果为false 3.Date对象 getTime()返回1970年1 ...

  5. stm8s103 EEPROM烧程序时能否保留

    EEPROM的参数需要再烧录程序时保留,做试验测试是否能够保留 1.在ST Visual Develop中硬件仿真手动修改EEPROM的值. 2.在ST Visual Programmer中读取EEP ...

  6. 路飞学城Python-Day14

    转载:python之路-路飞学城-python-book [25.常用模块-logging模块详解] [26.常用模块-logging模块详解2] [27.常用模块-logging模块日志过滤和日志文 ...

  7. oralce模糊查询之含有通配符

    oracle中通配符有 '_'和'%'当like  '_ww%'时,会把'_'和'%'当作通配符使用导致查不出含有'_'和'%'的数据.这时用到转译字符 like '\_ww\%' escape '\ ...

  8. VB学习生成JavaBean

    Application.ActiveWorkbook.Path 获取当前excel文件所在的文件地址 Excel VBA中表示当前工作簿,有Activeworkbook和Thisworkbook 两种 ...

  9. Node_进阶_7

    Node进阶第七天 一.复习 一.索引   数据库中,根据一个字段的值,来寻找一个文档,是很常见的操作.比如根据学号来找一个学生.这个学号是唯一的.只要有学号,就能唯一确认一个学生的文档.学号这个属性 ...

  10. HISTFILESIZE与HISTSIZE的区别

    在linux系统中,history命令可以输出历史命令,历史命令默认保存在文件~/.bash_history中. HISTFILESIZE 与 HISTSIZE都是history命令需要用到的两个sh ...