Straightforward Java Solution


  • 0
    R
    public class Solution {
        public int findComplement(int num) {
            int res = 0;
            int cnt = 0;
            while (num != 0) {
                int temp = ((num & 1) ^ 1) << cnt;
                res |= temp;
                num >>= 1;
                cnt++;
            }
            return res;
        }
    }
    

Log in to reply
 

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