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 示例 ...
随机推荐
- 快速排序及三向切分快排——java实现
快速排序也是一种分治算法.主要思想是选取一个切分点,将大于切分点的元素都放置到数组右侧,小于切分点的元素都放置到数组左侧:然后递归,再对切分点左侧和右侧分别排序. 归并排序时递归在前,归并在后,快速排 ...
- 原生JS---6
原生js学习笔记6——事件 事件对象 鼠标事件 event.clientX在可视区中,鼠标点击的x坐标 event.clientY在可视区中,鼠标点击的y坐标 示例: <!DOCTYPE htm ...
- Kettle环境变量配置
KETTLE_DIR=安装目录 KETTLE_HOME=安装目录 安装目录比如:D:\Kettle\pdi-ce-6.0.0.0-353\data-integration
- django模型层(二)
多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...
- ACM_名字的价值
名字的价值 Time Limit: 2000/1000ms (Java/Others) Problem Description: 集训终于开始了,参加集训的人很多,也就有很多名字,集训组织者发现了一件 ...
- C - Haiku
Problem description Haiku is a genre of Japanese traditional poetry. A haiku poem consists of 17 syl ...
- Last-Modified If-Modified-Since ETag If-None-Match
Last-Modified.If-Modified-Since 有些数据随时都在变化. CNN.com 的主页经常几分钟就更新.另一方面,Google.com 的主页几个星期才更新一次 (当他们上传特 ...
- js实现“级联菜单”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HIVE 命令记录
HIVE 命令记录 设置hive运行的队列 hive> set mapreduce.job.queuename=ven12; 打印列名 hive> set hive.cli.print.h ...
- openMSP430之openmsp430-loader
openmsp430-loader This simple program allows the user to load the openMSP430 program memory with an ...