o(1) solution in Java (no loops)


  • 0
    G
    public class Solution {
        public int findComplement(int num) {
            int flag = num;
            flag |= flag >> 1;
            flag |= flag >> 2;
            flag |= flag >> 4;
            flag |= flag >> 8;
            flag |= flag >> 16;
            flag |= flag >> 32;
            
            return ~(num) & flag;
        }
    }
    

Log in to reply
 

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