【一天一道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) return 1;

        if(x==-1&&n%2==0) return 1;

        if(x==-1&&n%2==1) return -1;

        if(n==-2147483648) return 1.0/(x*myPow(x,2147483647));

        if(n<0) return 1.0/myPow(x,0-n);

        if(n%2==0) return myPow(x*x,n/2);

        else return myPow(x*x,n/2)*x;

    }

};

时间: 2024-08-09 05:10:23

【一天一道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:

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】#89. Gray Code

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of