mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
17 lines
269 B
Go
17 lines
269 B
Go
package leetcode
|
|
|
|
func integerReplacement(n int) int {
|
|
res := 0
|
|
for n > 1 {
|
|
if (n & 1) == 0 { // 判断是否是偶数
|
|
n >>= 1
|
|
} else if (n+1)%4 == 0 && n != 3 { // 末尾 2 位为 11
|
|
n++
|
|
} else { // 末尾 2 位为 01
|
|
n--
|
|
}
|
|
res++
|
|
}
|
|
return res
|
|
}
|