美文网首页每天一道leetcode之入门
Day 23.Jewels and Stones(771)

Day 23.Jewels and Stones(771)

作者: 前端伊始 | 来源:发表于2018-05-04 14:48 被阅读0次

    问题描述: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".

    Examples:

    Input: J = "aA", S = "aAAbbbb"
    Output: 3
    

    思路:利用对象的属性来判断,把J中的每一个字符定义为某个对象的属性,然后遍历S判断S中的每一个字符是在这个对象的属性中

    /**
     * @param {string} J
     * @param {string} S
     * @return {number}
     */
    var numJewelsInStones = function(J, S) {
        var ret = {};
        for(j in J){
            ret[J[j]] = 1;
        }
        var num = 0;
        for(i in S){
            if(S[i] in ret){
                num++;
            }
        }
        return num;
    };
    

    文末彩蛋
    风靡一时的美国招兵海报


    Day 23.Jewels and Stones(771)

    相关文章

      网友评论

        本文标题:Day 23.Jewels and Stones(771)

        本文链接:https://www.haomeiwen.com/subject/inpdrftx.html