go: add «476. Number Complement»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a3fca26cdd
commit
c34927bd88
1 changed files with 11 additions and 0 deletions
11
go/number-complement.go
Normal file
11
go/number-complement.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package main
|
||||
|
||||
func findComplement(num int) int {
|
||||
mask := 0
|
||||
for tmp := num; tmp != 0; tmp >>= 1 {
|
||||
mask <<= 1
|
||||
mask |= 1
|
||||
}
|
||||
|
||||
return num ^ mask
|
||||
}
|
Loading…
Reference in a new issue