(num: i32)
| 5 | // 解释:5 的二进制表示为 101(没有前导零位),其补数为 010。所以你需要输出 2 。 |
| 6 | |
| 7 | pub fn find_complement(num: i32) -> i32 { |
| 8 | let mut binary_str: String = format!("{:b}", num); |
| 9 | binary_str = binary_str.replace("0", "-"); |
| 10 | binary_str = binary_str.replace("1", "0"); |
| 11 | binary_str = binary_str.replace("-", "1"); |
| 12 | isize::from_str_radix(&binary_str, 2).unwrap() as i32 |
| 13 | } |
| 14 | |
| 15 | fn main() { |
| 16 | println!("{:?}", find_complement(5)); |
nothing calls this directly
no outgoing calls
no test coverage detected