476. 数字的补数

This commit is contained in:
li_chx 2026-03-11 08:54:18 +08:00
parent e615d4f611
commit bde9d0f8b2
Signed by: li_chx
GPG Key ID: 70D4985BB8180E92
1 changed files with 6 additions and 6 deletions

View File

@ -4,17 +4,17 @@ struct Solution;
mod arr; mod arr;
impl Solution { impl Solution {
pub fn bitwise_complement(n: i32) -> i32 { pub fn find_complement(num: i32) -> i32 {
let mut a = 1; if num == 0{
for i in 1.. n.count_ones() + n.count_zeros() - n.leading_zeros() { 1
a |= 1 << i; }else {
num.bitxor((1 << (num.count_ones() + num.count_zeros() - num.leading_zeros())) - 1)
} }
n.bitxor(a)
} }
} }
fn main() { fn main() {
let result = Solution::bitwise_complement(0); let result = Solution::find_complement(0);
// let result = Solution::number_of_stable_arrays(200, 200, 25); // let result = Solution::number_of_stable_arrays(200, 200, 25);
println!("{:?}", result); println!("{:?}", result);
} }