Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 更新日志
    Search...
    ⌘K
    资源
    概览术语表系统提示词
    提示词库宇宙按键企业预言家网站向导Excel 公式专家Google Apps 脚本专家Python 除错大师时间旅行顾问故事创作助手引用来源SQL 魔法师解梦师双关语大师美食创作家混成词诗人幽默助手 HalLaTeX 达人情绪调色师Git 高手比喻达人伦理困境导航会议记录员习语解读者代码顾问函数生成器新词创造者CSV 转换器Emoji 编码器文章润色师多角度思考者知识问答生成器正念导师通俗化简述VR 健身创新者隐私信息净化器备忘录大师职业教练评分大师绕口令面试问题设计师语法精灵猜谜语代码解读器外星人类学家数据整理师品牌构建师效率评估器评论分类器方向解码器励志缪斯邮件提取器首席审核员课程规划师苏格拉底式智者头韵炼金师未来时尚顾问多语言超能力产品命名专家哲学沉思电子表格魔法师科幻场景模拟器自适应编辑器巴别塔广播推文语气检测器机场代码分析师
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    提示词库

    效率估算器

    计算函数和算法的时间复杂度。

    将此提示复制到我们的开发者控制台中亲自试用!

    内容
    System你的任务是分析提供的函数或算法,并使用大 O 表示法计算其时间复杂度。逐步解释你的推理过程,描述你是如何得出最终时间复杂度的。在确定时间复杂度时,请考虑最坏情况。如果函数或算法包含多个步骤或嵌套循环,请提供每个步骤的时间复杂度,然后给出整个函数或算法的总体时间复杂度。除非另有说明,否则假设使用的任何内置函数或操作的时间复杂度为 O(1)。
    Userdef example_function(n): for i in range(n): print(i) for j in range(n): for k in range(n): print(j, k)

    示例输出

    为了分析给定函数 example_function(n) 的时间复杂度,我们将其分解为多个步骤并考虑最坏情况。

    步骤 1:分析第一个循环

    for i in range(n):
       print(i)
    

    这个循环迭代 n 次,每次迭代执行一个常数时间操作(print(i))。因此,这一步的时间复杂度为 O(n)。

    步骤 2:分析嵌套循环

    for j in range(n):
        for k in range(n):
            print(j, k)
    

    外层循环迭代 n 次,每次迭代中,内层循环也迭代 n 次。在内层循环的每次迭代中,执行一个常数时间操作(print(j, k))。因此,嵌套循环的时间复杂度为 O(n * n) = O(n^2)。

    步骤 3:合并时间复杂度
    函数 example_function(n) 由两个步骤组成:第一个循环和嵌套循环。总体时间复杂度是每个步骤时间复杂度的总和。

    第一个循环的时间复杂度:O(n)
    嵌套循环的时间复杂度:O(n^2)

    为了确定总体时间复杂度,我们考虑主导项,即增长阶数最高的项。在这种情况下,O(n^2) 主导 O(n)。

    因此,函数 example_function(n) 的总体时间复杂度为 O(n^2)。

    在最坏情况下,当 n 很大时,嵌套循环将主导执行时间,函数将具有二次时间复杂度。


    API 请求

    import anthropic
    
    client = anthropic.Anthropic(
        # defaults to os.environ.get("ANTHROPIC_API_KEY")
        api_key="my_api_key",
    )
    message = client.messages.create(
        model="claude-opus-4-6",
        max_tokens=1000,
        temperature=0,
        system="Your task is to analyze the provided function or algorithm and calculate its time complexity using Big O notation. Explain your reasoning step by step, describing how you arrived at the final time complexity. Consider the worst-case scenario when determining the time complexity. If the function or algorithm contains multiple steps or nested loops, provide the time complexity for each step and then give the overall time complexity for the entire function or algorithm. Assume any built-in functions or operations used have a time complexity of O(1) unless otherwise specified.",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "def example_function(n):\n    for i in range(n):\n        print(i)\n\n    for j in range(n):\n        for k in range(n):\n            print(j, k)"
                    }
                ]
            }
        ]
    )
    print(message.content)
    

    Was this page helpful?

    • API 请求