1-line Java solution using bitwise operation


  • 0
    J
    public class Solution {
        public int findComplement(int num) {
            return ((Integer.highestOneBit(num) << 1) - 1) ^ num;
        }
    }
    

    Run time 10ms.


Log in to reply
 

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