MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / find_complement

Function find_complement

476-number-complement.rs:7–13  ·  view source on GitHub ↗
(num: i32)

Source from the content-addressed store, hash-verified

5// 解释:5 的二进制表示为 101(没有前导零位),其补数为 010。所以你需要输出 2 。
6
7pub 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
15fn main() {
16 println!("{:?}", find_complement(5));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected