0%

LeetCode 771. Jewels and Stones

題目

1
2
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3

解法思維

跑迴圈比對

1
2
3
4
5
6
7
8
9
10
11
12
var numJewelsInStones = function(jewels, stones) {
let c=0
for (const jew of jewels) {
for (const ston of stones) {
if(jew==ston){
c++
}
}

}
return c
};

先解構再用filter()篩選

1
2
3
4
var numJewelsInStones = function(jewels, stones) {
let s=[...stones].filter((item)=>jewels.includes(item))
return s.length
};