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

题目:

题解:

python 复制代码
class Solution:
    def lastRemaining(self, n: int) -> int:
        a1 = 1
        k, cnt, step = 0, n, 1
        while cnt > 1:
            if k % 2 == 0:  # 正向
                a1 += step
            else:  # 反向
                if cnt % 2:
                    a1 += step
            k += 1
            cnt >>= 1
            step <<= 1
        return a1
相关推荐
程序员敲代码吗19 分钟前
提升Python编程效率的五大特性
开发语言·python
List<String> error_P41 分钟前
Python蓝桥杯常考知识点-模拟
开发语言·python·蓝桥杯
比奇堡鱼贩1 小时前
python第五次作业
开发语言·前端·python
码农小韩2 小时前
AIAgent应用开发——DeepSeek分析(二)
人工智能·python·深度学习·agent·强化学习·deepseek
v_for_van2 小时前
力扣刷题记录6(无算法背景,纯C语言)
c语言·算法·leetcode
喵手2 小时前
Python爬虫实战:构建一个高健壮性的图书数据采集器!
爬虫·python·爬虫实战·零基础python爬虫教学·构建图书数据·采集图书数据·图书数据采集
样例过了就是过了3 小时前
LeetCode热题100 最大子数组和
数据结构·算法·leetcode
踩坑记录3 小时前
leetcode hot100 200. 岛屿数量 medium dfs
leetcode·深度优先
张3蜂3 小时前
Python venv 详解:为什么要用、怎么用、怎么用好
开发语言·python
老赵全栈实战3 小时前
《从零搭建RAG系统第3天:文档加载+文本向量化+向量存入Milvus》
python