1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// @Title: 数字的补数 (Number Complement)
// @Author: 15816537946@163.com
// @Date: 2021-10-18 00:27:16
// @Runtime: 0 ms
// @Memory: 1.9 MB
func findComplement(num int) int {
	if num == 0 {
		return 1
	}
	res := 0
	g := 1
	for num > 0 {
		if num&1 == 0 {
			res += g
		}
		g <<= 1
		num >>= 1
	}
	return res
}