1. 解法1
(遍历比较,时间复杂度O(J*S),空间复杂度O(1))
1.1 C#代码
public class Solution
{
public int NumJewelsInStones(string J, string S)
{
int Jlen = J.Length;
int Slen = S.Length;
int count = 0;
for(int i=0; i<Jlen;i++)
{
for(int j=0;j<Slen;j++)
{
if(S[j] == J[i])
++count;
}
}
return count;
}
}
1.2 Python3 代码
class Solution:
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
count = 0
for j in J:
for s in S:
if j == s:
count += 1
return count