[LeetCode]Power

周五的晚上,女朋友看爸爸去哪儿,电脑都是我的,狂刷LeetCode,做了十来题,最有印象的还是Power。题目如下:

Implement pow(xn).就是实现x的n次方。

这是一题很传统的题目了,但是之前没认真思考过。

第一个想法:比如计算pow(x, 100), 计算x,x^2,x^4,x^8,x^16,x^32,x^64.这些中间值都保存起来,再从后面开始遍历x^64*x^32*x^4;

要注意的是pow(xn)n有可能是0或者负数哦。

代码如下:

class Solution {
public:
    double pow(double x, int n) {
        if (0==n)
        {
            return 1;
        }
        if (n < 0)
        {
            n = -n;
            x = 1/x;
        }
        int current = 1;
        vector<double> pd;
        pd.push_back(x);
        while (current<<1 < n)
        {
            x = x*x;
            current = current<<1;
            pd.push_back(x);
        }
        int need = n-current;
        for (auto it = pd.rbegin(); need > 0 && it != pd.rend(); ++it)
        {
            if (current <= need)
            {
                x*=*it;
                need-=current;
            }
            current=current>>1;
        }
        return x;
    }
};

然后 Memory Limit Exceeded 了:(看来中间值不能保存啊!

如果不保存中间值,总不能计算pow(x, 100), 计算x,x^2,x^4,x^8,x^16,x^32,x^64;然后再计算x,x^2,x^4,x^8,x^16,x^32;然后再计算x,x^2,x^4。

100 = 64+32+4,先做分解再算,突然意识到这不就是二进制表示吗,于是代码就简单了:

class Solution {
public:
    double pow(double x, int n) {
        if (n == 0)
        {
            return 1;
        }
        if (n < 0)
        {
            n = -n;
            x = 1/x;
        }
        double result = 1;
        while (n > 0)
        {
            if (n&1==1)
            {
                result *= x;
            }
            x *= x;
            n = n >> 1;
        }
        return result;
    }
};

本来觉得这个代码已经够简洁了,在Disscus看到有人用递归,自己也写了一个,除了特殊情况的判断,就一句话:

class Solution {
public:
    double pow(double x, int n) {
        if (n == 0)
        {
            return 1;
        }
        if (n < 0)
        {
            n = -n;
            x = 1/x;
        }
        return n%2 ? pow(x*x, n>>1)*x : pow(x*x, n>>1);
    }
};
时间: 2024-10-12 21:50:53

[LeetCode]Power的相关文章

[LeetCode] Power of Three 判断3的次方数

Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. 这道题让我们判断一个数是不是3的次方数,在Le

[LeetCode] Power of Two 判断2的次方数

Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in O(1) time and using O(1) space? 这道题让我们判断一个数是否为2的次方数,而且要求时间和空间复杂度都为常数,那么对于这种玩数字的题,我们应该首先考虑位操作 Bit Operation.在LeetCode中,位操作的题有很多,比如比如Repeated DNA Seque

[LeetCode] Power of Four 判断4的次方数

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? Credits:Special thanks to @yukuairoy fo

[LeetCode] Power of Three

Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? 判断一个数是否是3的幂,则这个数a的所有的约数都是3的幂,如果一个数b小于这个数a且是3的幂,则这个数b一定是a的约数.所以找出3的最大的幂,然后用这个数对n取余即可. class Solution { public: boo

[LeetCode] Power of Two

Given an integer, write a function to determine if it is a power of two. 判断一个整数是否是2的幂.如果一个数是2的幂,则这个数的二进制表示中,最高位是1,其他位都是0.这个数-1的二进制位全是1.所以我们可以利用这个规律对两个数取与进行判断. class Solution { public: bool isPowerOfTwo(int n) { if (n <= 0) return false; return !(n &

[LeetCode] Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 判断一个数是否是4的幂,如果一个数是4的幂,则这个数的二进制的1在偶数位上.所以

[LeetCode]Power of N

题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是2的k次方数. 思路: 2的次方数使用2进制表示则必然只有最高位是1其他都是0: 这样判断一个数最多需要循环32次就能得出结果. 则程序如下: bool isPowerOfTwo(int n){ if (!n)return false; while ((n&0x01) != 0x01){//2的次方

LeetCode——Power of Two

Description: Given an integer, write a function to determine if it is a power of two. public class Solution { public boolean isPowerOfTwo(int n) { boolean flag = false; if(n == 0) return false; while(true) { if(n == 1) { flag = true; break; } if(n %

LeetCode Power of Two (2的幂)

题意:判断1个数n是否刚好是2的幂,幂大于0. 思路:注意会给负数,奇数.对于每个数判断31次即可. 1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 if(n<1||(n&1)==1&&n>1) return false; 5 unsigned int t=1; 6 while(t<n) //注意爆int 7 t<<=1; 8 if(t==n) return true; 9 ret