Leetcode 3199 (Ruby的硬伤)

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容