[LeetCode]50. Pow(x, n)

Problem Description

[https://leetcode.com/problems/powx-n/]
Implement pow(x, n).

思路

不明白這種題爲什麼會是mid的。。。

Code

package q050;

public class Solution {
    public double myPow(double x, int n) {
        if (n == 0)
            return 1;
        if(n<0){
            x=1/x;
            n=-n;
        }
        if (n % 2 == 0) {
            return myPow(x * x, n / 2);
        } else {
            return myPow(x, n - 1) * x;
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章