Recursive Java Solution


  • 0
    Y
    class Solution {
        int flipped = 0;
        public int findComplement(int num) {
            if(num == 0)
                return 1;
            recurse(num);
            return flipped;
        }
        
        private void recurse(int num) {
            if(num == 0)
                return;
            recurse(num / 2);
            flipped <<= 1;
            flipped += 1 - (num % 2);
        }
    }
    

Log in to reply
 

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