mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 01:15:57 +08:00
19 lines
253 B
Go
19 lines
253 B
Go
package leetcode
|
|
|
|
func lastRemaining(n int) int {
|
|
start, dir, step := 1, true, 1
|
|
for n > 1 {
|
|
if dir { // 正向
|
|
start += step
|
|
} else { // 反向
|
|
if n%2 == 1 {
|
|
start += step
|
|
}
|
|
}
|
|
dir = !dir
|
|
n >>= 1
|
|
step <<= 1
|
|
}
|
|
return start
|
|
}
|