[LeetCode] 50. Pow(x, n) Java

题目:

Implement pow(xn).

Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

题意及分析:实现求x的n次方,使用分治法,复杂度降低到log2n

代码:

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

    private double pow(double x, int n) {        //计算x的n次方
        if(n==0) return 1.0;
        double tmp=pow(x,n/2);
        if(n%2==0)
            return tmp*tmp;
        else
            return tmp*tmp*x;
    }
}

原文地址:https://www.cnblogs.com/271934Liao/p/8335156.html

时间: 2024-08-11 09:29:18

[LeetCode] 50. Pow(x, n) Java的相关文章

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)

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) (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

leetcode 50 pow(x,y)

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/4 = 0.25 Note:

19.2.7 [LeetCode 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/4 = 0.25 Note:

leetcode 50. Pow(x, n)(快速幂)

就是一个二分法快速幂. 但是需要注意的问题是这里是实数,而且n可能为负.int的范围是-2,147,483,648 至 2,147,483,647.如果为-2,147,483,648那么直接n=-n就爆int了.所以先要把n换成longlong. class Solution { public: double myPow(double x, int n) { double ans = 1; unsigned long long p; if (n < 0) { p = -n; x = 1 / x;

LeetCode:Pow(x, n)

1.题目名称 Pow(x, n)(求指定数字x的整数次幂) 2.题目地址 https://leetcode.com/problems/powx-n/ 3.题目内容 英文:Implement pow(x, n) 中文:给定底数x和指数n,求x的n次幂 4.解题方法1 在Java中,有一个偷懒的办法是这样实现的: /**  * 功能说明:LeetCode 50 - Pow(x, n)   * 开发人员:Tsybius2014  * 开发时间:2015年8月8日  */ public class So

leetcode 118 Pascal&#39;s Triangle ----- java

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 用的比价暴力的方法, 也是最快的. public class Solution { List list = new ArrayList<List<Integer>>(); public

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