LeetCode-1318. Minimum Flips to Make a OR b Equal to c

网友投稿 290 2022-08-29

LeetCode-1318. Minimum Flips to Make a OR b Equal to c

Given 3 positives numbers ​​a​​​, ​​b​​​ and ​​c​​​. Return the minimum flips required in some bits of ​​a​​​ and ​​b​​​ to make ( ​​a​​​ OR ​​b​​​ == ​​c​​​ ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

Example 1:

Input: a = 2, b = 6, c = 5 Output: 3 Explanation: After flips a = 1 , b = 4 , c = 5 such that (​​a​​​ OR ​​b​​​ == ​​c​​)

Example 2:

Input: a = 4, b = 2, c = 7Output: 1

Example 3:

Input: a = 1, b = 2, c = 3Output: 0

Constraints:

​​1 <= a <= 10^9​​​​1 <= b <= 10^9​​​​1 <= c <= 10^9​​

​​题解:​​

class Solution {public: int minFlips(int a, int b, int c) { int res = 0; for (int i = 0; i < 32; i++) { int k1 = a & (1 << i); int k2 = b & (1 << i); int k3 = c & (1 << i); if (k3 == 0) { if (k1 != k3) { res++; } if (k2 != k3) { res++; } } else { if (k1 != k3 && k2 != k3) { res++; } } } return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:grin中pow算法-cuckoo cycle的lean算法简单分析
下一篇:如何策划一场出色的品牌营销?(如何品牌策划和营销)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~