您的位置:首页 > 其它

LeetCode之771:Jewels and Stones

2018-02-28 13:51 507 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode Easy