-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01318. Minimum Flips to Make a OR b Equal to c.rs
51 lines (44 loc) · 1.61 KB
/
01318. 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
use std::cmp;
impl Solution {
pub fn min_flips(a: i32, b: i32, c: i32) -> i32 {
// Convert all numbers to binary strings
let mut a_str = format!("{:b}", a);
let mut b_str = format!("{:b}", b);
let mut c_str = format!("{:b}", c);
let mut a_ch;
let mut b_ch;
let mut c_ch;
let mut flips = 0;
let biggest_string_size = cmp::max(cmp::max(a_str.len(), b_str.len()), c_str.len());
// Convert all binary strings to equal lengths, prepend 0s at the start
while (a_str.len() != biggest_string_size) {
a_str = format!("0{}", a_str);
}
while (b_str.len() != biggest_string_size) {
b_str = format!("0{}", b_str);
}
while (c_str.len() != biggest_string_size) {
c_str = format!("0{}", c_str);
}
for i in 0..biggest_string_size {
a_ch = a_str.chars().nth(i).unwrap();
b_ch = b_str.chars().nth(i).unwrap();
c_ch = c_str.chars().nth(i).unwrap();
// If c is 0, count number of 1s that need to be flipped
if(c_ch == '0') {
if (a_ch == '1') {
flips += 1;
}
if (b_ch == '1') {
flips += 1;
}
} else if (c_ch == '1') {
// If c is 1, make sure both a and b are not 0. If they are, flip 1
if ((a_ch == '0' && b_ch == '0')) {
flips += 1;
}
}
}
return flips;
}
}