letCode(771 Jewels and Stones )
问题描述:
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
};
上一篇: 在Vue项目里面使用d3.js
下一篇: 3DMAX简单制作一朵纯洁的百合花