mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
27 lines
422 B
Go
27 lines
422 B
Go
package leetcode
|
|
|
|
import (
|
|
"math/bits"
|
|
)
|
|
|
|
// 解法一 模拟
|
|
func concatenatedBinary(n int) int {
|
|
res, mod, shift := 0, 1000000007, 0
|
|
for i := 1; i <= n; i++ {
|
|
if (i & (i - 1)) == 0 {
|
|
shift++
|
|
}
|
|
res = ((res << shift) + i) % mod
|
|
}
|
|
return res
|
|
}
|
|
|
|
// 解法二 位运算
|
|
func concatenatedBinary1(n int) int {
|
|
res := 0
|
|
for i := 1; i <= n; i++ {
|
|
res = (res<<bits.Len(uint(i)) | i) % (1e9 + 7)
|
|
}
|
|
return res
|
|
}
|