LeetCode 476. 数字的补救

476. 数字的补救

主要注意取反头有补码表示正负

  1. highestOneBit(num)最高为置1,-1以后首位为0其它为1。
  2. &保证补码为0不干扰数字
class Solution {
    public int findComplement(int num) {
        return ~num&(Integer.highestOneBit(num)-1);
    }
}

猜你喜欢

转载自blog.csdn.net/u014239185/article/details/85235947