美文网首页
Jewels-and-Stones

Jewels-and-Stones

作者: 过往云技 | 来源:发表于2019-01-17 23:05 被阅读0次

    给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

    J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。

    Go 实现

    func numJewelsInStones(J string, S string) int {
        num := 0
        for i:=0; i<len(J); i++ {
            for j := 0; j < len(S); j++ {
                if J[i] == S[j] {
                    num += 1
                }
            }
        }
        
        return num
    }
    

    PHP 实现

    function numJewelsInStones(string $str1, string $str2):int
    {
        $num = 0;
        $str1Len = strlen($str1);
        $str2Len = strlen($str2);
        for($i = 0 ; $i < $str1Len; $i++){
            for($j = 0; $j < $str2Len; $j++){
                if($str1[$i] == $str2[$j]){
                    $num += 1;
                }
            }
        }
        
        return $num;
    }
    
    echo(numJewelsInStones('aA', 'aAAbbbb'));
    

    相关文章

      网友评论

          本文标题:Jewels-and-Stones

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