LeetCode宝石与石头(C语言)
2019-03-19 17:20
447 查看
给定字符串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; }
相关文章推荐
- Leetcode:宝石与石头
- LeetCode 第771题 宝石与石头/Jawels and Stones
- 【LeetCode】宝石与石头
- Leetcode771宝石与石头
- LeetCode 刷题(python)之 宝石与石头
- Leetcode 771.宝石与石头
- [C语言][LeetCode][283]Move Zeroes
- leetcode 7. Reverse Integer(C语言,翻转一个整数,判断是否溢出)19
- leetcode 031--------Next Permutation------C语言
- leetcode 3. Longest Substring Without Repeating Characters的c语言解决
- [LeetCode练习题-C语言]168. Excel Sheet Column Title
- LeetCode 第三题两数之和 C语言
- [C语言] leetcode 119. Pascal's Triangle II
- leetcode 21. Merge Two Sorted Lists(C语言,合并两个排序的链表为一个排序链表)32
- Leetcode c语言-String to Integer (atoi)
- 6---LeetCode【tag: Array】【Rotate Array】|C语言|总结
- 【leetcode-7】Reverse Integer(C语言)
- 7---LeetCode【tag: Array】【Stock I】|C语言|总结
- LeetCode-33-Search in Rotated Sorted Array(C语言实现)
- LeetCode 242. Valid Anagram C语言