problem:

Implement pow(x, n).

Hide Tags

Math Binary
Search

题意:求x的n次幂

thinking:

(1)最简单想到的是直观上的数学幂函数求法,測试通过。算法时间复杂度为O(n)

(2)依照标签提示,使用二分搜索法。

pow(x,n) = pow(x,n-n/2)*pow(x,n/2),每次对n的规模缩半,注意对n的奇偶进行讨论,算法时间复杂度为log(n)

(3)除了上述方法,这里还提到了一种十分巧妙而且高速的方法,原文描写叙述例如以下:



Consider the binary representation of n. For example, if it is "10001011", then x^n = x^(1+2+8+128) = x^1 * x^2 * x^8 * x^128. Thus, we don't want to loop n times to calculate x^n. To speed up, we loop through each bit, if the i-th bit is 1, then we add x^(1
<< i) to the result. Since (1 << i) is a power of 2, x^(1<<(i+1)) = square(x^(1<<i)). The loop executes for a maximum of log(n) times.



该方法通过扫描n的二进制表示形式里不同位置上的1,来计算x的幂次,最坏为O(n),但平均复杂度非常好

code:

(1)递归法:accepted

class Solution {
public:
double pow(double x, int n) {
double ret=1.0;
if(x==1.0 )
return 1.0;
if(x==-1.0)
{
if(n%2==0)
return 1.0;
else
return -1.0;
}
if(n<0)
return 1.0/pow(x,-n);
while(n)
{
if(ret==0) //防止执行超时
return 0;
ret*=x;
n--;
}
return ret;
}
};

(2)二分法:accepted

class Solution {
public:
double pow(double x, int n) {
//double ret=1.0;
if(x==1.0 )
return 1.0;
if(x==-1.0)
{
if(n%2==0)
return 1.0;
else
return -1.0;
}
if(n==0)
return 1.0;
if(n<0)
return 1.0/pow(x,-n);
double half=pow(x,n>>1);
if(n%2==0)
return half*half;
else
return x*half*half;
}
};

(3)

为了正确计算x的n次幂,还须要考虑到下面一些情况:

1) x取值为0时。0的正数次幂是1,而负数次幂是没有意义的;推断x是否等于0不能直接用“==”。

2) 对于n取值INT_MIN时。-n并非INT_MAX。这时须要格外小心。

3) 尽量使用移位运算来取代除法运算,加快算法运行的速度。

class Solution {
public:
double pow(double x, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n<0)
{
if(n==INT_MIN)
return 1.0 / (pow(x,INT_MAX)*x);
else
return 1.0 / pow(x,-n);
}
if(n==0)
return 1.0;
double ans = 1.0 ;
for(;n>0; x *= x, n>>=1)
{
if(n&1>0)
ans *= x;
}
return ans;
}
};

leetcode || 50、Pow(x, n)的更多相关文章

  1. 力扣Leetcode 50. 实现Pow(x, n)

    实现Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 ...

  2. 50、[源码]-Spring容器创建-Bean创建完成

    50.[源码]-Spring容器创建-Bean创建完成 11.finishBeanFactoryInitialization(beanFactory);初始化所有剩下的单实例bean: beanFac ...

  3. leetcode 50. Pow(x, n) 、372. Super Pow

    50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...

  4. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  5. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

  6. LeetCode - 50. Pow(x, n)

    50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...

  7. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  8. Java实现 LeetCode 50 Pow(x,n)

    50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...

  9. LeetCode 50 - Pow(x, n) - [快速幂]

    实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...

随机推荐

  1. Shuffle'm Up(串)

    http://poj.org/problem?id=3087 题意:每组3个串,前两个串长度为n,第三个串长度为2*n,依次从第二个串(s2)中取一个字符,从第一个串(s1)中取一个字符,...... ...

  2. yii的criteria的用法

    Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个代码 ...

  3. html5 窗口之间的通信

    一般窗口通信分为三种: iframe嵌套:多个iframe之间通信. 父页面操作子页面元素:oFrame.contentWindow.document.body. 父页面调用子页面方法:oFrame. ...

  4. [Apple开发者帐户帮助]三、创建证书(1)证书概述

    在开发应用程序的过程中,您将创建不同的证书类型,以便在不同的上下文中使用.您将为iOS,tvOS和watchOS应用程序使用相同的证书集,并为macOS应用程序使用不同的证书集.您将使用开发证书在设备 ...

  5. python 5:str(某一变量)(将其他数字解释为字符串)

    age = messege = "Your's age is " + str(age) #将其他数字更改为字符串 print(messege) 运行结果应该是: Your's ag ...

  6. Spring的AOP机制---- AOP环绕通知---- AOP环绕通知

    323232三个人个地方司法发送哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

  7. Oracle 中文排序

        按照拼音顺序(常用)     ORDER BY nlssort(NAME, 'NLS_SORT=SCHINESE_PINYIN_M') 按照部首顺序 ORDER BY nlssort(NAME ...

  8. font使用

    font连写属性 font-style  font-variant font-weight  font-size/line-height  font-family font-size与font-fam ...

  9. python3设置打开文件的编码

    f = open(file_path,'r',encoding='utf8') 用起来很方便,不需要先读取再转码了.

  10. 创建一个类Person

    创建一个类Person,包含以下属性:姓名(name).年龄(age).朋友(friends数组).问候(sayhi方法,输出问候语,例如:"你好!").交朋友(addFriend ...