Solution in C++


  • 0
    F
    class Solution {
    public:
    	int findComplement(int num) {
    		int ret = 0, k = 0;
    
    		while (num)
    		{
    			int bit = 1 - (num & 1);
    
    			ret = ret | (k == 0 ? bit : (bit << k));
    			num >>= 1;
    
    			++k;
    		}
    
    		return ret;
    	}
    };
    

Log in to reply
 

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