LeetCode - 宝石与石头 (No.771)

771 - 宝石与石头

  • date : Dec.31st, 2019
  • platform : windows

problem description

给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jewels-and-stones
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

thinking

Just traverse. :-)

code

As follow, if more efficient, communicate with [email protected], thanks :-);

test

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    // test example 1
    // char *J = "aA";
    // char *S = "aAAbbbb";
    // test example 2
    // char *J = "z";
    // char *S = "ZZ";
    // int nCnt;

    nCnt = numJewelsInStones(J, S);

    printf("%d", nCnt);

    return 0;
}

function

int numJewelsInStones(char * J, char * S)
{
    int cnt;
    char *pJ, *pS;

    cnt = 0;

    for (pJ = J; *pJ != '\0'; pJ++)
    {
        for (pS = S; *pS != '\0'; pS++)
        {
            if (*pS == *pJ)
            {
                cnt++;
            }
        }
    }

    return cnt;
}

猜你喜欢

转载自www.cnblogs.com/litun/p/12127840.html