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;
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue