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;
    }
};
相关推荐
weixin_486681141 小时前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言1 小时前
C++入门
开发语言·c++
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜1 小时前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
代码小狗Codog2 小时前
C++独立开发开源大数计算库 CBigNum
数据结构·c++
WenGyyyL3 小时前
力扣最热一百题——二叉树的直径
java·c++·算法·二叉树·深度优先
sdlkjaljafdg3 小时前
vector<bool>性能测试
开发语言·c++·算法
telllong3 小时前
使用llama.cpp 在推理MiniCPM-1.2B模型
c++·llama·llama.cpp
huanxiangcoco4 小时前
55. 跳跃游戏
python·leetcode
__AtYou__4 小时前
Golang | Leetcode Golang题解之第413题等差数列划分
leetcode·golang·题解