原题链接:https://leetcode.com/problems/powx-n/submissions/
递归实现,注意int 的最小值转化为正数时出现的溢出问题。
AC 7ms beats 100% java:
class Solution {
public double myPow(double x, int n) {
if(n==0)
return 1;
if(n == Integer.MIN_VALUE){
return myPow(x*x, n/2);
}
if(n<0){
n=-n;
x=1/x;
}
return (n%2==0)?myPow(x*x,n/2):x*myPow(x*x,n/2);
}
}