19.2.7 [LeetCode 50] Pow(x, n)

Implement pow(xn), 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:

  • -100.0 < x < 100.0
  • n is a 32-bit signed integer, within the range [−231, 231 − 1]

 1 class Solution {
 2 public:
 3     double myPow(double x, long n) {
 4         if (n == 0)return 1;
 5         int _n = n;
 6         if (n < 0)n = -n;
 7         double pow = myPow(x, n / 2), ans;
 8         if (n % 2)
 9             ans = pow * pow*x;
10         else
11             ans=pow * pow;
12         if (_n < 0)
13             return 1 / ans;
14         return ans;
15     }
16 };

有点妙哦,但还是很慢

原文地址:https://www.cnblogs.com/yalphait/p/10355471.html

时间: 2024-07-29 01:49:15

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

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, n) Java

题目: Implement pow(x, n). 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); e

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:

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

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

【LeetCode】Pow(x, n)

Implement pow(x, n). 思路:快速幂运算,需要考虑指数为负数,同时底数为0的情况,这种属于异常数据,代码里没有体现. class Solution { public: double pow_abs(double x, unsigned int n) { if (n == 0) { return 1; } if (n == 1) { return x; } double res = pow(x, n>>1); res *= res; if (n & 0x1 == 1)