https://oj.leetcode.com/problems/powx-n/

二分法

class Solution {
public:
double pow(double x, int n) {
double ans = ; if(n == )
return 1.0;
if(n == )
return x;
bool isNegative = false;
if(n < )
{
isNegative = true;
n = - * n;
}
double n1 = n / ;
double n2 = n - n1*;
double t1 = pow(x,n1); //recursively
double t2 = pow(x,n2); //recursilvely
ans = t1*t1*t2;
if(isNegative)
ans = 1.0/ans; return ans;
}
};

LeetCode OJ-- Pow(x, n) **@的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. LeetCode OJ:Pow(x, n) (幂运算)

    Implement pow(x, n). 幂运算,简单的方法snag然很好实现,直接循环相乘就可以了,但是这里应该不是那种那么简单,我的做法使用到了一点递归: class Solution { pub ...

随机推荐

  1. CSS-标准盒模型 & 怪异盒模型

    CSS中Box model分类 CSS中Box model是分为两种:: W3C标准 和 IE标准盒子模型. 大多数浏览器采用W3C标准模型,而IE中则采用Microsoft自己的标准. 怪异模式是“ ...

  2. javascript隐藏和显示元素以及清空textarea

    当前希望写一个单选框,选中“paste”则显示粘贴框,选中“upload”则提示选择文件. 因为这两种情况只是显示不同,所以只需要用javascript来进行显示和隐藏. 最后的结果大概这样: 初始时 ...

  3. layer父页面调用子页面的方法

    由于不知道如何在子页面获取到layer定义的确定按钮,于是就在子页面上定义了一个方法,然后在由父页面在点确定按钮时调用子页面所定义的这个方法,从而执行子页面方法里面的内容: 子页面代码: functi ...

  4. sql中一个服务器建立另一个服务器的连接

    EXEC sp_addlinkedserver 'TonyLink','','SQLOLEDB','111.111.1.111(服务器名)' EXEC sp_addlinkedsrvlogin 'To ...

  5. lua table长度解析

    先来看lua table源码长度获取部分(ltable.c) j是数组部分的长度.首先判断数组长度大于0,并且数组最后一个是nil,就用二分法查找,返回长度. 如果t->node是 table的 ...

  6. 排查和处理一台被攻击的linux系统及其事后分析

    11:40 2018/3/16 发现最近几天服务器流量异常的大,检查了系统命令发现命令最近的修改时间很近,检查dns配置也发现了异常的dns服务器地址. 考虑到事态的严重性,铲掉这个系统重新搭建. 事 ...

  7. loj2073 「JSOI2016」扭动的回文串

    ref 主要是要理解"撑到"最长这个概念 (为啥我的代码这么长QAQ #include <iostream> #include <cstdio> using ...

  8. 解决pymysql不能实时查询最新的数据

    #在网上查询到的原因为: InnoDB 的默认隔离级别.它可以防止任何被查询的行被其他事务更改,从而阻止不可重复的读取,而不是 幻读取.它使用中度严格的锁定策略,以便事务内的所有查询都会查看同一快照中 ...

  9. [oldboy-django][2深入django]分页功能

    1 django自带分页 1.1 分页模板 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  10. mvcs项目搭建

    项目结构 下载链接:http://pan.baidu.com/s/1hsGtShA