classSolution1{ fun findComplement(num: Int): Int { var n = num var count = 0 while (n != 0) { n = n shr 1 count++ } var x = 1 var y = 1 count-- while (count != 0) { y = (x shl 1) + 1 x = y count-- } return num xor y } }
classSolution2{ fun findComplement(num: Int): Int { var n = num var t = 1 while (n != 0) { n = n ushr 1 t = t shl 1 } t-- return num xor t } }