C++ | Leetcode C++题解之第390题消除游戏

题目:

题解:

cpp 复制代码
class Solution {
public:
    int lastRemaining(int n) {
        int a1 = 1;
        int k = 0, cnt = n, step = 1;
        while (cnt > 1) {
            if (k % 2 == 0) { // 正向
                a1 = a1 + step;
            } else { // 反向
                a1 = (cnt % 2 == 0) ? a1 : a1 + step;
            }
            k++;
            cnt = cnt >> 1;
            step = step << 1;
        }
        return a1;
    }
};
相关推荐
南岩亦凛汀23 分钟前
快速上手Ultimate++的编译链接和配置
c++·gui·开源框架
TracyCoder12330 分钟前
LeetCode Hot100(13/100)——238. 除了自身以外数组的乘积
算法·leetcode
CoderCodingNo32 分钟前
【GESP】C++五级练习题 luogu-P3353 在你窗外闪耀的星星
开发语言·c++·算法
Anastasiozzzz35 分钟前
LeetCode Hot100 215. 数组中的第K个最大元素
数据结构·算法·leetcode
让我上个超影吧36 分钟前
【力扣76】最小覆盖子串
算法·leetcode·职场和发展
Howrun77743 分钟前
C++ 类间交互
开发语言·c++
近津薪荼1 小时前
优选算法——双指针5(单调性)
c++·学习·算法
2401_857683541 小时前
C++代码静态检测
开发语言·c++·算法
2401_838472511 小时前
内存泄漏自动检测系统
开发语言·c++·算法
GHL2842710901 小时前
Docker Desktop 启动报错“Virtualization support not detected“
c++·docker·容器