博客
关于我
leetcode题解50-Pow(x,n)
阅读量:799 次
发布时间:2023-01-31

本文共 1740 字,大约阅读时间需要 5 分钟。

要实现pow(x, n)函数,我们需要考虑很多特殊情况和优化点。

特殊情况处理

  • n为0:不管x是什么,结果都是1。
  • n为正偶数:结果为正数。
  • x为0:当n>0,结果为0。
  • 代码逻辑

    public class Solution {    public double myPow(double x, int n) {        // 处理n为0的情况        if (n == 0)            return 1.0;        // 处理x为-1的情况        if (Math.Abs(x) == 1.0) {            if ((x > 0) && (n % 2 == 0))                return 1.0;            if (x < 0 && (n % 2 != 0))                return -1.0;            return 1.0;        }        // 处理x为1的情况        if (Math.Abs(x) == 1.0) {            return x == 1.0 ? 1.0 : -1.0;        }        // 处理微小的浮点数误差        if ((x > 1.0 && x < 2.0) || (x < -1.0 && x > -2.0)) {            // 特殊处理接近于1的值            if ((n > 0 && x == 1.0) || (n < 0 && x == -1.0)) {                return 1.0;            }        }        // 处理n为正数的情况        if (n > 0) {            double result = x;            int m = n;            // 逐步计算,避免数值过大导致溢出的问题            do {                result *= x;                // 检查结果是否趋近于0                if (result < 1.0 / 1024.0)                    return 0.0;            } while (m-- > 0);            return result;        } else {            // 处理n为负数的情况            int m = -n;            do {                try {                    result *= result;                    // 检查是否结果趋近于0                    if (result < 1.0 / 1024.0)                        return 0.0;                } catch (OverflowException) {                    // 检查结果是否会溢出                    if (result > 1.7976931348623157e+308)                        return 0.0;                }            } while (m-- > 0);            // 取倒数            return 1.0 / result;        }    }}

    优化点

    • 数字处理:当计算结果趋向于0时,提前终止循环。
    • 避免溢出:使用双精度数并检查计算结果是否接近最大值。
    • 特殊值处理:快速处理x为1和-1的情况,以及n为0的情况,确保代码高效运行。
    • 减少循环次数:通过对数运算或者位处理,减少循环次数,提高性能。

    转载地址:http://pegyk.baihongyu.com/

    你可能感兴趣的文章
    mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
    查看>>
    Mysql8 数据库安装及主从配置 | Spring Cloud 2
    查看>>
    mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
    查看>>
    MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
    查看>>
    MYSQL8.0以上忘记root密码
    查看>>
    Mysql8.0以上重置初始密码的方法
    查看>>
    mysql8.0新特性-自增变量的持久化
    查看>>
    Mysql8.0注意url变更写法
    查看>>
    Mysql8.0的特性
    查看>>
    MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    查看>>
    MySQL8修改密码的方法
    查看>>
    Mysql8在Centos上安装后忘记root密码如何重新设置
    查看>>
    Mysql8在Windows上离线安装时忘记root密码
    查看>>
    MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
    查看>>
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>
    mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
    查看>>
    MYSQL:基础——3N范式的表结构设计
    查看>>