Java solution. O(n)


  • 0
    K

    I have extracted digit by digit, flipped it using xor operator and then appended it to the result:
    class Solution {
    public int findComplement(int num) {
    int a=0,n,i=0;
    while(num>=1)
    {
    n=num & 1;
    a+=(n ^ 1)* (int)Math.pow(2,i);
    i++;
    num=num>>1;
    }
    return a;
    }
    }


Log in to reply
 

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