50.Pow(x,n)+快速幂
题目:
思路
①暴力递归,但这样时间复杂度是O(n)
②递归快速幂:拆解2^n = 2^(n/2) * 2^(n/2),这样时间复杂度是O(log(n))
代码:
class Solution {
public double myPow(double x, int n) {
if(n < 0){
x = 1 / x;
n = Math.abs(n);
}
return count(x, n);
原创
2020-12-26 15:58:49 ·
59 阅读 ·
0 评论