LeetCode宝石与石头(C语言)

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

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

示例 1:

输入: J = "aA", S = "aAAbbbb"
输出: 3

示例 2:

输入: J = "z", S = "ZZ"
输出: 0

注意:

S 和 J 最多含有50个字母。
J 中的字符不重复。

// t.c.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "string.h"
int numJewelsInStones(char* J, char* S) {
    int count=0,i=1;
    char *p=J;
    char *p1=p;
    p1++;
    while(*p&&*p1)  //p指针指向J的首位,p1初始化是指向p后一位
    {
    	while(*p1)  
    	{
    		if(*p==*p1)
    		{
    	      return 0;		//若J有相同的字母就退出程序
    		}
    		p1++; //
    	}
    	p++;
    	p1=p;
    	p1++;
    }
    if((strlen(J)>50)||(strlen(S)>50))
    {
    	return 0;
    }
	char *s=J;
    char *s1=S;
	
	while(*s) //s指针指向J,s1指针指向S
	{
	  while(*s1)  
	  {
		  if(*s==*s1)  count++;
	       s1++;
	  }
	  s1=S;
	  s++;
	}
	return count;
    
}

int _tmain(int argc, _TCHAR* argv[])
{
	int n=  numJewelsInStones("aA","ABCDEFga");
  printf("%d",n);
  getchar();
	return 0;
}



在这里插入图片描述
红包+折扣,阿里云上云大礼包!
https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=5wzgtzow
【全民云计算】 云主机低至4折
https://promotion.aliyun.com/ntms/act/qwbk.html?userCode=5wzgtzow
【阿里云新用户】 云通信专享8折
https://www.aliyun.com/acts/alicomcloud/new-discount?userCode=5wzgtzow
【商标注册服务】 低至680
https://tm.aliyun.com/?userCode=5wzgtzow

发布了54 篇原创文章 · 获赞 40 · 访问量 241万+

猜你喜欢

转载自blog.csdn.net/tomy2426214836/article/details/88667532