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;
        } else {
            p = n;
        }
        while (p) {
            if (p & 1)
                ans *= x;
            x *= x;
            p >>= 1;
        }
        return ans;
    }
};
时间: 2024-07-29 01:49:13

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: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

hihoCoder 1143 : 骨牌覆盖问题&#183;一(递推,矩阵快速幂)

[题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘.对于这个棋盘,一共有多少种不同的覆盖方法呢? 举个例子,对于长度为1到3的棋盘,我们有下面几种覆盖方式: 提示:骨牌覆盖 提示:如何快速计算结果 输入 第1行:1个整数N.表示棋盘长度.1≤N≤100,000,000 输出 第1行:1个整数,表示覆盖方案数 M

矩阵快速幂 HDU3483

1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 //矩阵大小上限 7 const int SIZ=100; 8 int MOD; 9 10 //矩阵大小为n*m,初始化全部为0 11 struct mat 12 { 13 int n,m; 14 long long ar[SIZ][SIZ]; 15 mat() 16 { 17 memset(ar,0,sizeof(ar)); 18 n=m