码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-31 23:19:59      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:class   one   oid   aaa   block   UNC   tco   windows   授权   

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 leetun2001@outlook.com, 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;
}

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

标签:class   one   oid   aaa   block   UNC   tco   windows   授权   

原文地址:https://www.cnblogs.com/litun/p/12127840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!