Simple Java without API


  • 0
    N
    public class Solution {
        public int findComplement(int num) {
            int i = 0;
            while (num >> i++ > 0) ;    // find the leftmost bit of 1
            return num ^ ((1 << i-1)-1);    // num XOR all 1 in count of i
        }
    }
    

Log in to reply
 

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