[leedcode 50] Pow(x, n)

public class Solution {
    public double myPow(double x, int n) {
        //利用二分法,通过递归加速计算
        //注意:1.判断n是否为负
        // 2.递归结束条件,n==1和n==0
        // 3.n的奇偶不同处理方式

        if(n<0)
           return 1/pow(x,-n);
        else
           return pow(x,n);
    }
    public double pow(double x,int n){
        if(n==0) return 1;
        if(n==1) return x;
        double temp=pow(x,n/2);
        if(n%2==0){
            return temp*temp;
        }else{
            return temp*temp*x;
        }
    }
}
时间: 2024-10-13 02:08:41

[leedcode 50] Pow(x, n)的相关文章

50. Pow(x, n)

/* * 50. Pow(x, n) * 2016-5-7 by Mingyang * Divide and Conquer 分成子问题------>recursive * Complexity is log(n), as he is dividing n by half all the way. * 我开始自己写的代码很烂,因为我并没有做到完全的divede成half,因为我不断地重复那个过程 * 后面改进的代码就克服了这个问题 */ public static double myPow1(d

【javascript】50. Pow(x, n)

50. Pow(x, n) javascript使用var声明数据变量,变量没有固定的类型,如果需要使用整数,使用parseInt()进行转换. 1 var myPow = function(x, n) { 2 if(n==0)return 1; 3 if(n<0){ 4 n=-n; 5 x=1/x; 6 } 7 return (n%2==0) ? myPow(x*x,n/2) : (x*myPow(x*x,parseInt(n/2))); 8 };

50. Pow(x, n)(js)

50. Pow(x, n) Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

[Leetcode]50. Pow(x, n)

Implement pow(x, n). #define EPSINON 0.00001 #define Max 2147483647 #define Min -2147483648 #define DBL_MAX 1.7976931348623159e+308 class Solution { public: double myPow(double x, int n) { /* three special case */ if(n==1) return x; if(n==0) return 1

50. Pow(x, n) (INT; Divide-and-Conquer)

Implement pow(x, n). class Solution { public: double pow(double x, int n) { if(n == 0) return 1; return divideConquer(x,n); } double divideConquer(double x, int n) { double result; if(n==1) return x; if(n == -1) return 1/x; if(n%2 == 0) { result = di

LeetCode#50 Pow(x, n)

Just... Implement pow(x, n). Solution: 1)Naive solution:multiply x by itself for n-1 times. (Or simply reyurn 1 if n==0). This takes O(n) time. When n is big enough, it's relatively slow. 2)Fast Power. Take pow(3, 11) as an example. Here's what we're

【一天一道LeetCode】#50. Pow(x, n)

一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回值为1:x==-1时,根据n的奇偶来判断 3.n==-2147483648,特殊情况,int的范围时-2147483648-2147483647, */ class Solution { public: double myPow(double x, int n) { if(n==0||x==1) r

leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法

Pow(x, n) Implement pow(x, n). 思路:题目不算难,但是需要考虑的情况比较多. 具体代码如下: public class Solution { public double myPow(double x, int n) { boolean isMin0 = true;//结果负号 if(x > 0 || (n&1) == 0){//x>0或n为偶数 isMin0 = false;//为正 } x = x < 0 ? -x:x;//将x统一设为正值 dou