Implement pow(xn).

Hide Tags

Math Binary Search

 

  题目很简单的。
 
class Solution {
public:
double pow(double x, int n) {
if(n==) return ;
bool nNeg = false;
long long int nn = n;
if(n<){
nn = - nn ;
nNeg =true;
}
bool xNeg = false;
if(x<){
x = -x;
if(n%==)
xNeg = true;
}
double ret = ;
while(nn){
if(nn&)
ret *= x;
x *=x;
nn>>=;
}
if(nNeg==true)
ret = /ret;
if(xNeg==true)
return -ret;
return ret;
}
};

[LeetCode] Pow(x, n) 二分搜索的更多相关文章

  1. LeetCode:算法特辑——二分搜索

    LeetCode:算法特辑——二分搜索 算法模板——基础 int L =0; int R =arr.length; while(L<R) { int M = (R-L)/2+L; if(arr[ ...

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

    Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...

  3. leetcode pow(x,n)实现

    题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...

  4. LeetCode: pow

    Title: https://leetcode.com/problems/powx-n/ 思路:二分.使用递归或者非递归.非递归有点难理解.pow(0,0)=1 递归的方法是将n为负数的用除法解决.有 ...

  5. [leetcode]Pow(x, n) @ Python

    原题地址:https://oj.leetcode.com/problems/powx-n/ 题意:Implement pow(x, n). 解题思路:求幂函数的实现.使用递归,类似于二分的思路,解法来 ...

  6. Leetcode: Pow(x, n) and Summary: 负数补码总结

    Implement pow(x, n). Analysis:  Time Complexity: O(LogN) Iterative code: refer to https://discuss.le ...

  7. [LeetCode] Pow(x, n)

    Implement pow(x, n). 有史以来做过最简单的一题,大概用5分钟ac,我采用fast exponential,这个在sicp的第一章就有描述.思想是:如果n是偶数的话,那么m^n = ...

  8. [LeetCode] Pow(x, n) (二分法)

    Implement pow(x, n). 刚开始没想到,后来看remlost的博客才写出来,代码很简练: class Solution { public: double pow(double x, i ...

  9. leetcode Pow(doubule x,int n)

    今天第一天开通博客,心情还是小激动的 上代码: 方法一:常规递归,x的n次方={xn/2*xn/2              //n为偶 xn/2*xn/2 *x          //n为奇数 } ...

随机推荐

  1. 记一次https访问握手失败(handshake failure)

    文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6239518.html  转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...

  2. 详解Bootstrap按钮组件

    按钮组也是一个独立的组件,所以可以找到相应的源码文件: Less:buttons.less Sass:_buttons.scss Css:Bootstrap.css    3131行~3291行 按钮 ...

  3. android: SQLite删除数据

    删除数据对你来说应该就更简单了,因为它所需要用到的知识点你全部已经学过了. SQLiteDatabase 中提供了一个 delete()方法专门用于删除数据,这个方法接收三个参数,第一 个参数仍然是表 ...

  4. sqlserver内存释放心得

    SQL Server 2008 或者R2的默认内存分配是2147483647MB, 差不多算是无穷大,对于系统内存的管理策略是有多少占多少.SQLserver会把所有处理过的SQL操作缓存在内存里,这 ...

  5. android 平台搭建

    直接到官网下载 http://developer.android.com/sdk/index.html?utm_source=weibolife

  6. The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread

    01-Jul-2016 14:25:30.937 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoade ...

  7. object-c学习1

    因为公司需要,开始看object-c,虽然还没ios系统,但现学下语法. 第一个例子不应该是helloWorld吗?但<Learn Objective-C on the Mac>书上不是. ...

  8. TStringList中AddObject使用

    结构体定义 PYpType=^TYpType; TYpType=record    yfcode:string;    ypcode:string;    YpUnitPrice:Currency;  ...

  9. 在JS中设置Select和radio选中

    <select id="Gender" name="Gender"> <option value="1">男< ...

  10. Visual Studio 2013 prerequisites

    http://www.visualstudio.com/zh-cn/products/visual-studio-ultimate-with-msdn-vs#Fragment_SystemRequir ...