Java works for both negative and positive integers


  • 0
    A
    class Solution {
        public int findComplement(int num) {
            if(num < 0)
            {
                 int ones = (Integer.highestOneBit(num) << 1)  +1;
                return (num ^ ones);
            }
             int ones = (Integer.highestOneBit(num) << 1)  - 1;
             return num ^ ones;
        }
    }
    

Log in to reply
 

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