Simplest Solution on leetcode C++


  • 0
    A
    class Solution {
    public:
        int findComplement(int num) {
                    int sol = 0;
    		int next_set_bit = 1;
    		int lastbit;
    		while (num) {
    			lastbit = num & 1;
    			if (lastbit == 0) {
    				sol |= next_set_bit;
    			}
    			next_set_bit = next_set_bit << 1;
    			num = num >> 1;
    		}
    		return sol;
        }
    };
    

Log in to reply
 

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