编程语言
首页 > 编程语言> > LeetCode题解(1318):或运算的最小翻转次数(Python)

LeetCode题解(1318):或运算的最小翻转次数(Python)

作者:互联网

题目:原题链接(中等)

标签:位运算

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( l o g N ) O(logN) O(logN) O ( 1 ) O(1) O(1)44ms (33.33%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def minFlips(self, a: int, b: int, c: int) -> int:
        ans = 0

        length = max(a.bit_length(), b.bit_length(), c.bit_length())
        for i in range(length):
            if c & (1 << i):
                if not a & (1 << i) and not b & (1 << i):
                    ans += 1
            else:
                if a & (1 << i):
                    ans += 1
                if b & (1 << i):
                    ans += 1

        return ans

标签:Python,题解,复杂度,1318,int,length,Ans,bit
来源: https://blog.csdn.net/Changxing_J/article/details/112858701