Leetcode 50. Pow(x, n) x的n次方 in Java
50. Pow(x, n)
- Total Accepted: 106878
- Total Submissions: 388728
- Difficulty: Medium
Implement pow(x, n).
public class Solution { public double myPow(double x, int n) { int sign=1; if(n<0){ sign=-1; n=-n; } return sign<0? 1/pow(x,n) : pow(x,n) ; } public double pow(double x,int n){ if(n==0) return 1; if(n==1) return x; if(n%2==0) { double tmp=pow(x,n/2); return tmp*tmp; }else{ double tmp=pow(x,n/2); return tmp*tmp*x; } } }
浙公网安备 33010602011771号