3199. 用偶數(shù)異或設置位計數(shù)三元組 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