技术栈

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

Mopes__2024-09-05 20:03

题目:

题解:

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
上一篇:C++ | Leetcode C++题解之第393题UFT-8编码验证
下一篇:LeetCode: 521. 最长特殊序列 Ⅰ
相关推荐
xlp666hub
6 分钟前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
ZhengEnCi
3 小时前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风
9 小时前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风
9 小时前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly
1 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling
1 天前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook
1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风
1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风
1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei
2 天前
python 抽象基类
python
热门推荐
01GitHub 镜像站点02OpenClaw 使用和管理 MCP 完全指南03Window 10部署openclaw报错node.exe : npm error code 12804OpenClaw + 飞书(Feishu)环境搭建指南05OpenClaw 连接飞书完整指南:插件安装、配置与踩坑记录06本地部署 OpenClaw + DeepSeek-R1 完全指南07Claude Code + GLM4.7 避坑指南:解决 Unable to connect to Anthropic services08小黑课堂计算机二级WPSoffice题库软件下载安装教程(2026年3月最新版)09OpenClaw 飞书机器人不回复消息?3 小时踩坑总结10Clawdbot部署教程:解决‘gateway token missing’授权问题的完整步骤