class Solution {
public:
int numJewelsInStones(string J, string S) {
int i;
for (auto c:J) //通过基于范围的for语句访问字符串中每一个字符(C++11标准)
for (auto d:S)
if(c==d)
i++;
return i;
}
};
然而LeetCode上提交出错,拿到VS上运行是无误的!
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
int numJewelsInStones(string J, string S)
{
int i=0;
for (auto c : J) //通过基于范围的for语句访问字符串中每一个字符(C++11标准)
for (auto d : S)
if (c == d)
i++;
return i;
}
};
int main()
{
string J, S;
int num;
cout << "input J:" << endl;
cin >> J;
cout << "input S:" << endl;
cin >> S;
num = Solution().numJewelsInStones(J, S);
cout << num << endl;
system("pause");
}