您的位置:首页 > 其它

lintcode:Pow(x, n)

2016-03-16 19:03 260 查看

Pow(x, n)

Implement pow(x, n).

解题

public class Solution {
/**
* @param x the base number
* @param n the power number
* @return the result
*/
public double myPow(double x, int n) {
// Write your code here
if(x == 0)
return 0;
if(n == 0)
return 1.0;
if( n<0)
return 1.0/(myPow(x,-n));
double res = x;
while(n>1){
res*=x;
n--;
}
return res;
}
}


Java Code

class Solution:
# @param {double} x the base number
# @param {int} n the power number
# @return {double} the result
def myPow(self, x, n):
# Write your code here
return x**n
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: