-
Notifications
You must be signed in to change notification settings - Fork 8
/
Solution.kt
48 lines (43 loc) · 1.27 KB
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Created by Inno Fang on 2017/12/21.
*/
class Solution {
fun findComplement(num: Int): Int {
if (num == 0 || num == 1) return 0
var num = num
var order = (Math.log10(num.toDouble()) / Math.log10(2.0)).toInt()
var complement = IntArray(order)
while (num != 0 && order != 0) {
complement[--order] = if ((num and 1) == 0) 1 else 0
num = num shr 1
}
return complement.reduce { acc, i ->
(acc shl 1) or i
}
}
}
class Solution2 {
fun findComplement(num: Int): Int {
var mask = 0.inv()
while ((num and mask) != 0) mask = mask shl 1
return mask.inv() and num.inv()
}
}
class Solution3 {
fun findComplement(num: Int): Int {
var mask = num
mask = mask or (mask shr 1)
mask = mask or (mask shr 2)
mask = mask or (mask shr 4)
mask = mask or (mask shr 8)
mask = mask or (mask shr 16)
return num xor mask
}
}
fun main(args: Array<String>) {
Solution().findComplement(5).let(::println)
Solution().findComplement(2).let(::println)
Solution().findComplement(1).let(::println)
Solution().findComplement(Int.MAX_VALUE).let(::println)
Solution().findComplement(0).let(::println)
}