Leetcode Note: Go - Complement of Base 10 Integer

Complement of Base 10 Integer - LeetCode
https://leetcode.com/problems/complement-of-base-10-integer/

  • Go 言語で取り組んだメモ

回答

Go solution faster than 100% bit manuplation - LeetCode Discuss
https://leetcode.com/problems/complement-of-base-10-integer/discuss/1059496/Go-solution-faster-than-100-bit-manuplation

func bitwiseComplement(N int) int {
	if N == 0 {
		return 1
	}
	res := 0
	g := 1
	for N > 0 {
		if N&1 == 0 {
			res += g
		}
		g <<= 1
		N >>= 1
	}
	return res
}