求数组的整数次方
思想:
分而治之
首先判断正负数,然后判断奇偶性问题:
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/powx-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);double rest = myPow(x,n%2);return rest*half*half;}
};
class Solution {public double myPow(double x, int n) {if(n==0||x==1) return 1;if(x==0) return 0;if(n<0) return 1/(myPow(x,-(n+1))*x);double num=myPow(x,n/2);if(n%2==0){ return num*num;}else{return num*num*x;}}
}