leetcode || 50、Pow(x, n)
problem:
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)的更多相关文章
- 力扣Leetcode 50. 实现Pow(x, n)
实现Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 ...
- 50、[源码]-Spring容器创建-Bean创建完成
50.[源码]-Spring容器创建-Bean创建完成 11.finishBeanFactoryInitialization(beanFactory);初始化所有剩下的单实例bean: beanFac ...
- 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 ...
- [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 ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- LeetCode 50 - Pow(x, n) - [快速幂]
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...
随机推荐
- Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...
- bzoj2822[AHOI2012]树屋阶梯(卡特兰数)
2822: [AHOI2012]树屋阶梯 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 879 Solved: 513[Submit][Status] ...
- Django day16 Auth组件
一:Auth组件 -django内置的用户认证系统,可以快速的实现,登录,注销,修改密码... -怎么用? (1)先创建超级用户: -python3 manage.py createsuperuser ...
- MYSQL 数据库命令行终端操作笔记
1.数据库登录: 1.登录本地的MYSQL数据库:mysql -u root -p 2.连接远程主机上的MYSQL数据库:mysql -h 192.168.191.2 -u root -p 123 ...
- C99新增内容之复合文字(compound literal)
前言: 最近在复习C,发现了一些新东西,例如:变长数组,复合文字,指针的兼容性等.今天先简单谈一下复合文字. 正文: 假如需要向带有一个int参量的函数传递一个值,您可以传递一个int变量,也可以传递 ...
- 解决QQ未启用状态,QQ留言图标未启用
最近由于腾讯升级QQ一些东西,导致QQ图标成未启用状态:如图 解决方法,到腾讯此站点登陆一下即可, http://wp.qq.com/set.html 另外设置 没有保存按钮,如果选择完全公开,到自己 ...
- Spring Cloud (1) 服务的注册与发现(Eureka)
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...
- 初学Hibernate杂乱总结
1.如果在"one"方中(如部门)写有Set属性,但是没有在映射文件中配置,那么,在获取指定部门下的所有员工时,不会报错,但是,Set内的元素个数为0.输出为"[]&qu ...
- vs2017 创建项目推送到Git上
地址 在从本地往云上推送的时候遇到了这样的问题 将分支推送到远程存储库时遇到错误: rejected Updates were rejected because the remote contains ...
- C++版的LLC代码
图像稀疏编码总结:LLC和SCSPM,文章对稀疏编码讲解非常详细. <Locality-constrained Linear Coding for Image Classification> ...