#力扣 LeetCode 50. Pow(x, n) #在所有 Java 提交中擊敗了 98.10% 的使用者 @FDDLC
阿新 • • 發佈:2020-12-16
技術標籤:演算法&資料結構
題目描述:
https://leetcode-cn.com/problems/powx-n/
Java程式碼:
class Solution { //-100.0 < x < 100.0 ,n 是 32 位有符號整數,其數值範圍是 [−2^31, 2^31 − 1] 。 public double myPow(double x, int n) { if(n==0)return 1; if(n==1)return x; if(n==-1)return 1.0/x; double answer=myPow(x,n>>1); answer*=answer; if((n&1)==1)answer*=x;//-5=(-3)+(-3)+1,5=2+2+1 return answer; } }