当前位置: 移动技术网 > IT编程>开发语言>JavaScript > letCode(771 Jewels and Stones )

letCode(771 Jewels and Stones )

2019年01月25日  | 移动技术网IT编程  | 我要评论
问题描述: 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 o ...

问题描述:

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
    };

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网