C solution


  • 0
    J
    int findComplement(int num)
    {
        int sum = 0;
        int t = 1;//二的零次方 == 1
        int shu;
        while (num != 0)
        {
            shu = (num ^ 1) - (((num ^ 1) >> 1) << 1);//位运算
            num = num >> 1;//位运算
            sum = sum + shu * t;//转化为十进制
            t = t * 2;
        }
        return sum;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.