Leetcode 50. Pow(x, n) (Medium) (cpp)

来源:互联网 发布:2k18詹姆斯身体数据 编辑:程序博客网 时间:2024/05/23 14:02

Leetcode 50. Pow(x, n) (Medium) (cpp)

Tag: Binary Search, Math

Difficulty: Medium


/*50. Pow(x, n) (Medium)Implement pow(x, n).*/class Solution {public:    double myPow(double x, int n) {        if (n == 0) {            return 1;        }        if (n < 0) {            if (n == INT_MIN) {                return 1 / x * myPow(x, n + 1);            }            x = 1 / x;            n = -n;        }        double temp = myPow(x, n / 2);        return n % 2 ? x * temp * temp : temp * temp;    }};


0 0
原创粉丝点击