476. 数字的补数
This commit is contained in:
parent
e615d4f611
commit
bde9d0f8b2
12
src/main.rs
12
src/main.rs
|
|
@ -4,17 +4,17 @@ struct Solution;
|
|||
mod arr;
|
||||
|
||||
impl Solution {
|
||||
pub fn bitwise_complement(n: i32) -> i32 {
|
||||
let mut a = 1;
|
||||
for i in 1.. n.count_ones() + n.count_zeros() - n.leading_zeros() {
|
||||
a |= 1 << i;
|
||||
pub fn find_complement(num: i32) -> i32 {
|
||||
if num == 0{
|
||||
1
|
||||
}else {
|
||||
num.bitxor((1 << (num.count_ones() + num.count_zeros() - num.leading_zeros())) - 1)
|
||||
}
|
||||
n.bitxor(a)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = Solution::bitwise_complement(0);
|
||||
let result = Solution::find_complement(0);
|
||||
// let result = Solution::number_of_stable_arrays(200, 200, 25);
|
||||
println!("{:?}", result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue