码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode - Pow(x, n)

时间:2015-12-26 11:32:23      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

题目:

Implement pow(x, n).

思路:

二分法

package math;

public class Pow {

    public double myPow(double x, int n) {
        if (n == 0) return 1;
        if (n == 1) return x;
        if (n == -1) return 1/x;
        int half = n / 2;
        double v = myPow(x, half);
        return v * v * myPow(x, n - half - half);
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Pow p = new Pow();
        System.out.println(p.myPow(2, 5));
    }

}

 

LeetCode - Pow(x, n)

标签:

原文地址:http://www.cnblogs.com/shuaiwhu/p/5077690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!