Leetcode1006笨阶乘

思路:以4为一个分组分别进行处理

python 复制代码
class Solution:
    def clumsy(self, n: int) -> int:
        answer_dict = {0:0,1: 1, 2: 2, 3: 6, 4: 7}
        if n > 4:
            answer = n * (n - 1) // (n - 2) + n - 3
            n -= 4
        else:
            print(answer_dict[n])
            return answer_dict[n]
        print(answer)
        while n >=4:
            current_answer = n * (n - 1) // (n - 2) - n + 3
            answer -= current_answer
            n -= 4
            print(answer,n)
        answer-=answer_dict[n]
        print(answer)
        return answer
相关推荐
心静财富之门9 分钟前
Python 数据共享四种方案完整实例(同进程 / JSON / Redis / HTTP)
数据库·python·oracle
低调滴开发28 分钟前
3 行代码写崩一个函数?切片才是 Rust 字符串处理的正确姿势
开发语言·后端·rust
CodeYp201937 分钟前
第15讲:二叉搜索树
开发语言·c++·二叉搜索树
王仔小奶39 分钟前
慢SQL治理与性能调优:DBA如何用Shell/Python提升运维效率?
python·sql·dba
用户03321266636743 分钟前
使用 Python 快速在 PowerPoint 中生成表格【教程】
python
猫猫.1531 小时前
HashTable、HashMap、ConcurrentHashMap 核心区别总结
java·开发语言·java-ee
夜晚回家1 小时前
Python 身份运算符详解 | is 与 == 到底差在哪
python
fengye2071611 小时前
cpython框架结构
python
用户91294046848421 小时前
大模型返回 JSON 后,先过这道 Python 校验
python
Dmz丶1 小时前
前端转 AI 100 天 Day 32:LLM 客户端工程化——重试、超时、降级、限流
python