Golang | Leetcode Golang题解之第390题消除游戏

题目:

题解:

Go 复制代码
func lastRemaining(n int) int {
    a1 := 1
    k, cnt, step := 0, n, 1
    for cnt > 1 {
        if k%2 == 0 { // 正向
            a1 += step
        } else { // 反向
            if cnt%2 == 1 {
                a1 += step
            }
        }
        k++
        cnt >>= 1
        step <<= 1
    }
    return a1
}
相关推荐
Tony Bai20 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
在风中的意志1 天前
[数据库SQL] [leetcode-584] 584. 寻找用户推荐人
数据库·sql·leetcode
毅炼1 天前
hot100打卡——day08
java·数据结构·算法·leetcode·深度优先
DICOM医学影像1 天前
15. Go-Ethereum测试Solidity ERC20合约 - Go-Ethereum调用合约方法
开发语言·后端·golang·区块链·智能合约·以太坊·web3.0
我要用代码向我喜欢的女孩表白1 天前
对象存储路径文件1TB以上文件比对,go语言
ios·golang·xcode
leoufung1 天前
LeetCode 67. Add Binary:从面试思路到代码细节
算法·leetcode·面试
无限进步_1 天前
【C语言】循环队列的两种实现:数组与链表的对比分析
c语言·开发语言·数据结构·c++·leetcode·链表·visual studio
linsa_pursuer1 天前
最长连续序列
java·数据结构·算法·leetcode
POLITE31 天前
Leetcode 54.螺旋矩阵 JavaScript (Day 8)
javascript·leetcode·矩阵
巴塞罗那的风1 天前
golang协程泄漏排查实战
开发语言·后端·golang