C++_AC_Bit Manipulation


  • 0

    We finally come back from the holiday season... lol

    class Solution {
    public:
    int findComplement(int num) {
        int ans = 0;
        int n = 0;
        int tmp = num;
        while(tmp){
            n++;
            tmp = tmp>>1;
        }
        tmp = 1<<(--n);
        while(tmp){
            int dig = (num & tmp)>>n;
            ans = (ans<<1) ^ (!dig);
            tmp = tmp>>1;
            n--;
        }
        return ans;
    }
    };

Log in to reply
 

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