[LeetCode]Pow(x, n)

来源:互联网 发布:java测试工程师面试题 编辑:程序博客网 时间:2024/04/24 05:36

题目

Number: 50
Difficulty: Medium
Tags: Math Binary Search

Implement pow(x, n).

题解

实现幂函数。

代码

double myPow(double x, int n) {    if( x == 1 || n == 0)        return 1;    double t = myPow(x, n / 2);    if(n % 2)        return n < 0 ? 1 / x * t * t : x * t * t;    else        return t * t;}
0 0
原创粉丝点击