-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution693.go
More file actions
33 lines (28 loc) · 845 Bytes
/
Copy pathsolution693.go
File metadata and controls
33 lines (28 loc) · 845 Bytes
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
package solution963
import (
"math/bits"
)
// ============================================================================
// 693. Binary Number with Alternating Bits
// URL: https://leetcode.com/problems/binary-number-with-alternating-bits/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/693---Binary-Number-with-Alternating-Bits
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_hasAlternatingBits
Benchmark_hasAlternatingBits-24 186342273 6.426 ns/op 0 B/op 0 allocs/op
PASS
*/
func hasAlternatingBits(n int) bool {
m := bits.LeadingZeros(uint(n))
for i := 0; i < 64-m; i++ {
b1 := n & (1 << i)
b2 := n & (1 << (i + 1))
if b1 == 0 && b2 == 0 || b1 != 0 && b2 != 0 {
return false
}
}
return true
}