3199. 用偶数异或设置位计数三元组 I
这题就是欺负Ruby没有O(1)时间复杂度的bit_count方法,直接模拟Python能过,Ruby不能过
# @param {Integer[]} a
# @param {Integer[]} b
# @param {Integer[]} c
# @return {Integer}
def triplet_count(a, b, c)
cnt = 0
a.each do |a1|
b.each do |b1|
c.each do |c1|
if (a1 ^ b1 ^ c1).to_s(2).count("1")%2 == 0
cnt += 1
end
end
end
end
cnt
end
Python
class Solution:
def tripletCount(self, a: List[int], b: List[int], c: List[int]) -> int:
cnt = 0
for a1 in a:
for b1 in b:
for c1 in c:
if int.bit_count(a1 ^ b1 ^ c1)%2 == 0:
cnt += 1
return cnt