首页 > 代码库 > [LeetCode]Pow(x, n)

[LeetCode]Pow(x, n)

Implement pow(xn).


public class Solution {
	public double pow(double x, int n) {
		if(n>0) return powInt(x,n);
		else return 1/powInt(x,-n);
	}
	
	private double powInt(double x, int n){
		if (n == 0)
			return 1;
		if (n == 1)
			return x;
		double p = pow(x, n / 2);
		if (n % 2 == 0) {
			return p * p;
		}else{
			return p*p*x;
		}
	}
}




[LeetCode]Pow(x, n)