[leetcode]50. Pow(x, n)

来源:互联网 发布:淘宝人群画像分析 编辑:程序博客网 时间:2024/06/10 20:20

题目链接:https://leetcode.com/problems/powx-n/#/description

Implement pow(xn).


class Solution {public:    double myPow(double x, int n) {        if (n == 0)            return 1.0;        else {            if (n < 0)            {                //判断是否溢出                if (n == INT_MIN)                    return 1.0 / (myPow(x, INT_MAX)*x);                else                    return 1.0 / myPow(x, -n);            }else{                if (n % 2 == 0)                {                    double temp = myPow(x, n >> 1);                    return temp * temp;                }                else{                    double temp = myPow(x, (n - 1) >> 1);                    return temp * temp * x;                }            }//else        }//else    }};


0 0
原创粉丝点击