给出两个数a和b,问二进制表示下flip最少几个bit,可以使得a OR b = c,简单位运算(有参考Discussion的写法)
1 #1318
2 #Runtime: 27 ms (Beats 6.6%)
3 #Memory: 13.3 MB (Beats 78.79%)
4
5 class Solution(object):
6 def minFlips(self, a, b, c):
7 """
8 :type a: int
9 :type b: int
10 :type c: int
11 :rtype: int
12 """
13 ans = 0
14 for i in range(31):
15 if (c >> i) & 1:
16 ans += ((a >> i) & 1) == 0 and ((b >> i) & 1) == 0
17 else:
18 ans += (a >> i) & 1
19 ans += (b >> i) & 1
20 return ans