-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPower.java
49 lines (42 loc) · 1.24 KB
/
Power.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.offer.case11;
/**
* 面试题 11:数值的整数次方
* Created by tanc on 2017/3/29.
*/
public class Power {
public static double methodOne(double base, int exponent) {
if (exponent == 0) {
return 1.0;
} else if (exponent > 0) {
return powerMethodTwo(base, exponent);
} else {
if (Double.compare(base, 0.0) == 0) {
throw new RuntimeException("非法输入");
}
return 1.0 / powerMethodTwo(base, 0 - exponent);
}
}
private static double powerMethodOne(double base, int exponent) {
double result = 1.0;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
private static double powerMethodTwo(double base, int exponent) {
if (exponent == 0) {
return 0;
} else if (exponent == 1) {
return base;
}
// 使用移位代替除 2 运算
double result = powerMethodTwo(base, exponent >> 1);
result *= result;
// exponent % 2 != 0
// 使用位运算代替取余运算
if ((exponent & 1) == 1) {
result *= base;
}
return result;
}
}