码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode //C - 50. Pow(x, n)


    50. Pow(x, n)

    Implement pow(x, n), which calculates x raised to the power n (i.e., x n x^n xn).
     

    Example 1:

    Input: x = 2.00000, n = 10
    Output: 1024.00000

    Example 2:

    Input: x = 2.10000, n = 3
    Output: 9.26100

    Example 3:

    Input: x = 2.00000, n = -2
    Output: 0.25000
    Explanation: 2 − 2 = 1 / 2 2 = 1 / 4 = 0.25 2^{-2} = 1/2^2 = 1/4 = 0.25 2−2=1/22=1/4=0.25

    Constraints:
    • -100.0 < x < 100.0
    • − 2 31 < = n < = 2 31 − 1 -2^{31} <= n <= 2^{31}-1 −231<=n<=231−1
    • n is an integer.
    • Either x is not zero or n > 0.
    • − 1 0 4 < = x n < = 1 0 4 -10^4 <= x^n <= 10^4 −104<=xn<=104

    From: LeetCode
    Link: 50. Pow(x, n)


    Solution:

    Ideas:
    1. The powHelper function recursively calculates the power for a non-negative exponent.
    2. For the case when n is negative, it calculates the power for the positive value of n and then returns the reciprocal of the result.
    3. The isNegative flag is used to check if the original n was negative.
    4. Special care is taken to avoid overflow when converting a negative n to its positive counterpart, especially important when n is INT_MIN.
    Code:
    double myPow(double x, int n) {
        // Helper function to calculate power for non-negative exponent
        double powHelper(double x, unsigned int n) {
            if (n == 0) return 1;
            double half = powHelper(x, n / 2);
            if (n % 2 == 0) 
                return half * half;
            else 
                return half * half * x;
        }
    
        // Handling the case when n is negative
        bool isNegative = false;
        unsigned int positiveN = n;
        if (n < 0) {
            isNegative = true;
            positiveN = -((unsigned int)n); // Convert to positive, handle overflow
        }
    
        double result = powHelper(x, positiveN);
    
        // If n is negative, return the reciprocal
        return isNegative ? 1 / result : result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    Linux 部分很实用的命令使用详解
    工业自动化控制通信协议Profinet系列-3、CoDeSys软PLC方案介绍
    如何为SD卡与NAND Flash的uboot加上menu菜单!
    Qt 之自定义控件(开关按钮)
    python第三次作业
    微服务架构,客户端如何catch服务端的异常?
    SpringCloud 组件Gateway服务网关【gateway快速入门】
    R数据分析:纵向分类结局的分析-马尔可夫多态模型的理解与实操
    自己的碎碎念集合
    码农的转型之路-PLC异地组网与远程控制
  • 原文地址:https://blog.csdn.net/navicheung/article/details/134522827
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号