-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path50_Pow.cpp
57 lines (54 loc) · 1.11 KB
/
50_Pow.cpp
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
50
51
52
53
54
55
56
57
/*
* @Author: [email protected]
* @Last Modified time: 2016-06-26 20:16:28
*/
class Solution {
public:
double myPow(double x, int n) {
// Simple recursively way.
if(n == 0){
return 1.0;
}
int half_abs = abs(n/2);
if(n>0){
double result = myPow(x*x, half_abs);
if(n&0x1 == 1){
result *= x;
}
return result;
}
else{
double result = 1/myPow(x*x, half_abs);
if(n&0x1 == 1){
result *= 1/x;
}
return result;
}
}
};
class Solution_2{
public:
double myPow(double x, int n) {
/* Another way: shorter code.
* According to: https://leetcode.com/problems/powx-n/
* Important here. Or if n == INT_MIN, it will overload.
*/
long ln = n;
if(ln==0){
return 1.0;
}
if(ln<0){
ln = -ln;
x = 1/x;
}
return ln&0x1==1 ? x * myPow(x*x, ln/2) : myPow(x*x, ln/2);
}
};
/*
8.88023
3
2.00000
-2147483648
2.2
-100
*/