-
Notifications
You must be signed in to change notification settings - Fork 2
/
minimum-flips-to-make-a-or-b-equal-to-c.rs
56 lines (48 loc) · 1.4 KB
/
minimum-flips-to-make-a-or-b-equal-to-c.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(3, Solution::min_flips(2, 6, 5));
}
struct Solution;
impl Solution {
pub fn min_flips1(a: i32, b: i32, c: i32) -> i32 {
let mut count = 0;
for i in 0..32 {
let a1 = ((a as u32) << (31 - i)) >> 31;
let b1 = ((b as u32) << (31 - i)) >> 31;
let c1 = ((c as u32) << (31 - i)) >> 31;
if c1 == 0 {
if (a1 == 1 && b1 == 0) || (a1 == 0 && b1 == 1) {
count += 1;
} else if a1 == 1 && b1 == 1 {
count += 2;
}
} else {
if a1 == 0 && b1 == 0 {
count += 1;
}
}
}
count
}
// 任何数 &1 都只会剩下最后位的数字
pub fn min_flips(a: i32, b: i32, c: i32) -> i32 {
let mut count = 0;
for i in 0..32 {
let a1 = (a >> i) & 1;
let b1 = (b >> i) & 1;
let c1 = (c >> i) & 1;
if c1 == 0 {
if (a1 == 1 && b1 == 0) || (a1 == 0 && b1 == 1) {
count += 1;
} else if a1 == 1 && b1 == 1 {
count += 2;
}
} else {
if a1 == 0 && b1 == 0 {
count += 1;
}
}
}
count
}
}