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 示例 ...
随机推荐
- Constructing Roads(spfa)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2493 #include <stdio.h ...
- 前端常见面试题总结part2
今天总结了几道,感觉非常有意思的题,有感兴趣的可以看下,有疑问请留言~ (答案在最后) 考察自执行函数的this指向 审题要细心 var n = 2, obj = { n:2, fn:(functio ...
- JavaScript 函数 伪数组 arguments
一.函数 函数:函数就是将一些语言进行封装,然后通过调用的形式,执行这些语句. 函数的作用: 1.将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动 2.简化编程,让变 ...
- xhtml1-transitional.dtd
<!-- Extensible HTML version 1.0 Transitional DTD This is the same as HTML 4 Transitional except ...
- C#之经理评分系统
PM类,几乎全是属性 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- ES6 学习小结1
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...
- 浅谈ByteBuffer转换成byte[]时遇到的问题
有些时候我们要把ByteBuffer转换成byte[]来使用.于是很多时候会用以下代码来转换: ByteBuffer buf; .....(一些往buffer写数据的操作) byte[] bs= ne ...
- Architecture:话说科学家/工程师/设计师/商人
从使命.目的.行为的不同,可以归类人群到科学家.工程师.设计师.商人等等.使命分别是:1.携带当下社会的财富对未来探索,希望引发变革:2.掌握工程全貌.完成整个工程的圣经周期:3.在工程的设计层面做文 ...
- 计算laws的matlab代码
很简单的代码:不过花了codeforge上的10个点,自己写也早写出来了; 代码如下: 文件:calLaws.m function [y,h_v,h_h]=calLaws(x,id,LocalEner ...
- 通用功能类:改变WinForm窗体显示颜色
一.显示窗体调用方法 protected override void OnLoad(EventArgs e) { MDIClientSupport.SetBevel ...