Leetcode刷题1-771.宝石与石头(C++)
2019-02-23 20:43
323 查看
题目来源:链接: [https://leetcode-cn.com/problems/jewels-and-stones/].
771.宝石与石头
1.问题描述
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
示例 1:
输入: J = “aA”, S = “aAAbbbb”
输出: 3
示例 2:
输入: J = “z”, S = “ZZ”
输出: 0
注意:
S 和 J 最多含有50个字母。
J 中的字符不重复。
2.解决方案
直接用两次循环暴力解决法
需要做两点异常处理:
- 考虑S和J是否为空,return -1;
- 若S和J所含字符大于50个,return -1;
3.代码
class Solution { public: int numJewelsInStones(string J, string S) { //异常处理 if(J == NULL || S == NULL || J.length() > 50 || S.length() > 50) { return -1; } int m = 0 , n = 0; int i = 0 , j = 0; int count = 0; m = J.length(); n = S.length(); for(i = 0;i < m; i++) { for(j = 0;j < n; j++) { if(J[i] == S[j]) { count++; } } } return count; } };
2019/2/23 胡云层 于南京 1
相关文章推荐
- Leetcode771宝石与石头
- Leetcode 771.宝石与石头
- LeetCode 第771题 宝石与石头/Jawels and Stones
- 【LeetCode】宝石与石头
- Leetcode:宝石与石头
- LeetCode 刷题(python)之 宝石与石头
- LeetCode宝石与石头(C语言)
- LeetCode刷题(C++)——Letter Combinations of a Phone Number(Medium)
- LeetCode 401. Binary Watch 题解(C++)
- LeetCode - 401. Binary Watch - 思路详解-C++
- 【LeetCode-88】Merge Sorted Array(C++)
- [C++]LeetCode: 45 Pow(x, n)
- 【C++】【LeetCode】70. Climbing Stairs
- LeetCode - 152. Maximum Product Subarray - 思路详解 - C++
- leetcode题解c++ | 37. Sudoku Solver
- LeetCode入坑第一题:twoSum (C++)
- leetcode 832. 翻转图像 c++
- C++ for leetcode起步
- leetcode 462 Minimum Moves to Equal Array Elements II C++
- [C++]LeetCode: 103 Jump Game (局部最优和全局最优法)