2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_50.java 912 Bytes
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-02-26 22:01 . 新增中等难度算法解析
package com.hit.basmath.learn.binary_search;
/**
* 50. Pow(x, n)
* <p>
* Implement pow(x, n), which calculates x raised to the power n (x^n).
* <p>
* Example 1:
* <p>
* Input: 2.00000, 10
* Output: 1024.00000
* <p>
* Example 2:
* <p>
* Input: 2.10000, 3
* Output: 9.26100
* <p>
* Example 3:
* <p>
* Input: 2.00000, -2
* Output: 0.25000
* <p>
* Explanation: 2^-2 = 1/2^2 = 1/4 = 0.25
* <p>
* Note:
* <p>
* -100.0 < x < 100.0
* <p>
* n is a 32-bit signed integer, within the range [−2^31, 2^31 − 1]
*/
public class _50 {
public double myPow(double x, int n) {
double temp;
if (n == 0) return 1;
temp = myPow(x, n / 2);
if (n % 2 == 0) {
return temp * temp;
} else {
if (n > 0) {
return x * temp * temp;
} else {
return (temp * temp) / x;
}
}
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助