Java - Easiest to understand solution without using in-built functions


  • 0
    G
    public class Solution {
        public int findComplement(int num) {
            int res = 0;
            int loc = 0;
            while(num != 0){
                int bit = num & 1;
                
                if(bit == 0){
                    res = res | 1 << loc;
                }
                loc++;
                
                num = num >>> 1;
            }
            return res;
        }
    }
    

Log in to reply
 

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