文章目录题目描述思路 && 代码二刷 题目描述 很棒的题目!便于分治思想的理解! 思路 && 代码 分情况是大头~递归结束的情况: x0x^0x0 = 1x1x^1x1 = xx−1^-1−1 = 1 / x 偶数情况:直接二分递归奇数情况:先向下取整二分递归,再补乘一个n即可 class Solution {// 正负数都满足情况public double myPow(double x, int n) {// 三个递归结束条件if(n == 0) { return 1; }if(n == 1) { return x; }if(n == -1) { return 1 / x; }// 递归代码double half = myPow(x, n / 2);// half 向下取整,加一个奇数情况判断double mod = myPow(x, n % 2);// x^(n/2) * x^(n/2) * x^(0 or 1) = x^nreturn half * half * mod;} } 二刷 class Solution {public double myPow(double x, int n) {if(n == 0) {return 1;}if(n == 1) {return x;}if(n == -1) {return 1 / x;}double half = myPow(x, n / 2);return half * half * myPow(x, n % 2);} }