您的位置:首页 > 其它

leetcode771.宝石与石头

2019-05-06 01:03 302 查看

leetcode771.宝石与石头

代码

//C++
class Solution {
public:
int numJewelsInStones(string J, string S) {
int ans = 0;
for (int i=0; i<J.length(); i++){
for (int j=0; j<S.length(); j++){
if (J[i]==S[j])
ans ++;
}
}
return ans;
}
};
# python2.7
class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
return sum(S.count(i) for i in J)

总结

  • python中a.count(x),计算x出现次数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: