LeetCode: pow
Title:
https://leetcode.com/problems/powx-n/
思路:二分。使用递归或者非递归。非递归有点难理解.pow(0,0)=1 递归的方法是将n为负数的用除法解决。有问题,没有考虑0的负数次幂会导致除数为0.对于非递归,可以这么理解,x是指数,不断增长,x , x^2, x^4,x^8
class Solution {
public:
double Pow(double x,int n){
if (n == )
return 1.0;
if (n == )
return x;
double a = Pow(x,n/);
if (n % == )
return a*a;
else
return a*a*x;
}
double myPow(double x,int n){
if (n < ){
if (x == 0.0)
return 0.0;
else
return 1.0/Pow(x,-n);
}
else{
return Pow(x,n);
}
}
};
class Solution {
public:
double myPow(double x, int n) {
if ( == n)
return 1.0;
if (n < ){
if (x == )
return 0.0;
else{
x = 1.0 / x;
n = - * n;
}
}
double result = 1.0;
while (n > ){
if (n & ){
result *= x;
}
n /= ;
x *= x;
}
return result;
}
};
LeetCode: pow的更多相关文章
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- leetcode pow(x,n)实现
题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...
- [leetcode]Pow(x, n) @ Python
原题地址:https://oj.leetcode.com/problems/powx-n/ 题意:Implement pow(x, n). 解题思路:求幂函数的实现.使用递归,类似于二分的思路,解法来 ...
- Leetcode: Pow(x, n) and Summary: 负数补码总结
Implement pow(x, n). Analysis: Time Complexity: O(LogN) Iterative code: refer to https://discuss.le ...
- [LeetCode] Pow(x, n)
Implement pow(x, n). 有史以来做过最简单的一题,大概用5分钟ac,我采用fast exponential,这个在sicp的第一章就有描述.思想是:如果n是偶数的话,那么m^n = ...
- [LeetCode] Pow(x, n) 二分搜索
Implement pow(x, n). Hide Tags Math Binary Search 题目很简单的. class Solution { public: double pow( ...
- [LeetCode] Pow(x, n) (二分法)
Implement pow(x, n). 刚开始没想到,后来看remlost的博客才写出来,代码很简练: class Solution { public: double pow(double x, i ...
- leetcode Pow(doubule x,int n)
今天第一天开通博客,心情还是小激动的 上代码: 方法一:常规递归,x的n次方={xn/2*xn/2 //n为偶 xn/2*xn/2 *x //n为奇数 } ...
- LeetCode Pow(x, n) (快速幂)
题意 Implement pow(x, n). 求X的N次方. 解法 用正常的办法来做是会超时的,因为可能有21亿次方的情况,所以需要优化一下.这里用到了快速幂算法,简单来说就是将指数分解成二进制的形 ...
随机推荐
- Spring+Mybatis+Maven 整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="by ...
- input点击删除默认value,以及只能输入数字,删除,tab
/*inputhastip类绑定事件*/ $('.inputhastip').css("color", "#999"); $('.inputhastip').b ...
- setDepthStencilState
cgfx->hlsl StencilFunc={always,1,0xff}->setDepthStencilState(DepthState,0) StencilFunc={always ...
- windows 64位整数
#include <iostream> #include <ctime> using namespace std; int main() { cout << cou ...
- js String Trim函数
<javascript> String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,"& ...
- UVA 10523 Very Easy!!!(大数据加法、乘法)
题意:给出N,A,计算i*A^i(i=1~N)的和.1<=N<=30,0<=A<=15. 就是大数据运算,别的没什么,注意细节之处即可. 这题还要注意两个地方: 1.考虑A=0 ...
- [Hibernate]dynamic-insert和dynamic-update属性
这二个属性默认情况均为false,你可以通过以下二种方式进行配置使用: 1.Annotation @Entity @Table(name = "stock_transaction" ...
- http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html(重要)
http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html
- 再谈PCA
其实之前写过PCA相关的博文,但是由于之前掌握的理论知识有限,所以理解也比较浅.这篇博文,我们以另外一种角度来理解PCA看,这里我假设大家对PCA都有一个初步的了解.首先,我们举一个二维空间中 ...
- seafile安装日志(非教程)
需要的软件: python 2.7.x(从 Seafile 5.1 开始,python 版本最低要求为2.7) python-setuptools python-imaging python-mysq ...