Java 31 to 0 position one scan solution


  • 1
    D
    public class Solution {
        public int findComplement(int num) {
            int ret = 0;
            boolean metone = false;
            for (int i = 31; i >= 0; i--) {
                int mask = 1 << i;
                int and = num & mask;
                if (and != 0 || metone) {
                    metone = (!metone && and != 0) ? true : metone;
                    ret = ret * 2 + ((metone && and == 0) ? 1 : 0);
                }
            }
            return ret;
        }
    }
    

Log in to reply
 

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