2220. Minimum Bit Flips to Convert Number ## 思路 檢查XOR之後的bit數 ## Code ```python class Solution: def minBitFlips(self, start: int, goal: int) -> int: res = 0 num = start ^ goal while num: res += 1 num &= num-1 return res ``` 1318. Minimum Flips to Make a OR b Equal to c ## 思路 檢查a,b,c的bit c=1 -- a,b至少要有一個是1, 皆為0就flip+1 c=0 -- 把a,b為1的bit都flip ## Code ```python class Solution: def minFlips(self, a: int, b: int, c: int) -> int: res = 0 for _ in range(32): if c & 1: res += 0 if (a & 1) or (b & 1) else 1 else: res += (a & 1) + (b & 1) a >>= 1 b >>= 1 c >>= 1 return res ``` -- https://i.imgur.com/kyBhy6o.jpeg
-- ※ 發信站: 批踢踢實業坊(pttweb.org.tw), 來自: 185.213.82.223 (臺灣) ※ 文章網址: https://pttweb.org.tw/Marginalman/M.1726027947.A.9BE