Implement pow(xn).

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

 class Solution {
public:
double myPow(double x, int n)
{
if(n == INT_MIN)
return 1.0/(x * myPow(x, INT_MAX)); //这里考虑了一下由于传参数的限制还是将其变成INT_MAX
if(n < )
return 1.0/myPow(x, -n);
if(n == )
return 1.0;
double half = myPow(x, n/), ret = 0.0;
if(n%)
return half * half * x;
return half * half;
}
};

这里有个注意点就是考虑到INT_MIN的情况,做出相应的处理

下面是java写的版本:

 class Solution {
public double myPow(double x, int n){
if(n == Integer.MIN_VALUE)
return 1.0/(x*myPow(x, Integer.MAX_VALUE));
if(n < 0)
return 1.0/myPow(x,-n);
if(n == 0)
return 1.0;
double half = myPow(x, n/2);
if(n%2 == 1)
return half * half * x;
return half * half;
}
}

LeetCode OJ:Pow(x, n) (幂运算)的更多相关文章

  1. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  2. LeetCode OJ 题解

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

  3. 求幂运算、多项式乘法及Horner法则的应用

    一,两种不同的求幂运算 求解x^n(x 的 n 次方) ①使用递归,代码如下: private static long pow(int x, int n){ if(n == 0) return 1; ...

  4. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

  5. POJ1026 Cipher(置换的幂运算)

    链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  6. 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)

    CARDS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1448   Accepted: 773 Description ...

  7. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  8. 【LeetCode OJ】Interleaving String

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

  9. 【LeetCode OJ】Reverse Words in a String

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

随机推荐

  1. 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  2. rsync+inotify实时同步

    !!!在安装前要先确保,rsync daemon服务配置成功,在安装inotify-tools前先确认你的linux内核是否达到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,也可以 ...

  3. 系统间接口联调总是报500 for URL 和 乱码

    两个系统做数据传输时,懒省事,直接访问 action 方式.结果总是报500,或者fileNotFount. 究其原因是因为两边的数据格式没对应上.post请求返回的格式是String,数据提供方返回 ...

  4. PAT 天梯赛 L1-001 【水】

    L1-001. Hello World 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 这道超级简单的题目没有任何输入. 你只需要在一行中输 ...

  5. Sybase数据库:两个特别注意的地方

    Sybase数据库:两个特别注意的地方 一.字段别名 字段别名不能为查询条件中的列名,会导致查询出来的数据不准确:最好字段别名为非列名: 二.更新的表名的大小写 update a set .... s ...

  6. 基于jQuery和Bootstrap的手风琴垂直菜单

    在线演示 本地下载

  7. MyBatis正在爬的坑

    换了份工作,开始接触Mybatis,开一篇文章记录一下自己遇到的坑 2018-06-20 今天遇到了一个问题,编好的sql语句在数据库可以执行但是写到程序里边就GG,什么问题呢?一直纠结在程序哪里写错 ...

  8. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  9. vs2013 浏览器 browserlink 不停访问

  10. MVC viewbag & viewdata

    弱类型:ViewData[""] 动态型:ViewBag dynamic ViewData 是字典型的(Dictionary),ViewBag 不再是字典的键值对结构,而是dyna ...