1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc50.java 640 Bytes
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-06-26 13:15 . 20190626
package code;
/*
* 50. Pow(x, n)
* 题意:幂运算
* 难度:Medium
* 分类:Math, Binary Search
* 思路:和lc29的思路类似,数值上的二分。用前一步的结果进行下一步的运算,降低了迭代次数
* Tips:注意溢出的情况,负数的情况。
*/
public class lc50 {
public double myPow(double x, int n) {
if(n == 0) return 1;
if(n==Integer.MIN_VALUE && x!=1 && x!=-1) return 0; //负数最小值,结果溢出
if(n<0){
n = -n;
x = 1/x; //负数变成 1/x
}
return (n%2 == 0) ? myPow(x*x, n/2) : x*myPow(x*x, n/2);
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891