給出兩個數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