Short easy understand C++ Solution


  • 0

    The comment is the code flow.

    class Solution {
    public:
        int findComplement(int num) {
            // mark found the left most '1'
            bool found=false;
            int res=0;
            for(int i=31;i>=0;i--){
                if(!found && !(num&(1<<i))){
                    continue;
                }
                found=true;
                if(!(num&(1<<i)))
                    res+=(1<<i);
            }
            
            return found?res:1;
        }
    };
    

Log in to reply
 

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