Simple Java Solution

  • 0

    "class Solution {
    public int findComplement(int num) {
    num=~num; //flip all the bits but this flips most significant 0's to 1's which we don't want.
    int checker = 1<<31; //number with highest set bit which will keep on shifting
    while((num&checker)!=0){//while most significant bits are 1 flip them
    num^=checker;/flipping most significant 1 bits till we find first 0 bit
    return num;

Log in to reply

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