mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
cs: add “1318. Minimum Flips to Make a OR b Equal to c”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
d06468acfe
commit
87b1174f4c
1 changed files with 21 additions and 0 deletions
21
cs/minimum-flips-to-make-a-or-b-equal-to-c.cs
Normal file
21
cs/minimum-flips-to-make-a-or-b-equal-to-c.cs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
public class Solution {
|
||||||
|
public int MinFlips(int a, int b, int c) {
|
||||||
|
var flips = 0;
|
||||||
|
|
||||||
|
for (; (a | b) != c; a >>= 1, b >>= 1, c >>= 1) {
|
||||||
|
var (aa, bb, cc) = (a & 1, b & 1, c & 1);
|
||||||
|
|
||||||
|
if ((aa | bb) == cc) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aa != 0 && bb != 0) {
|
||||||
|
flips += 2;
|
||||||
|
} else {
|
||||||
|
++flips;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return flips;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue