forked from GhanshyamSharma122/BitByBit-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path50.cpp
More file actions
39 lines (36 loc) · 608 Bytes
/
Copy path50.cpp
File metadata and controls
39 lines (36 loc) · 608 Bytes
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
class Solution {
public:
double myPow(double x, int n) {
double ans=1;
if (x==0.0)
{
return (0.0);
}
if (x==1.0)
{
return (1.0);
}
if (n<0)
{
x=1/x;
}
return powr(x,n);
}
double powr(double x,long n)
{
if (n==0)
{
return 1;
}
long halfn=n/2;
double half=powr(x,halfn);
if (n%2==0)
{
return half*half;
}
else
{
return half*half*x;
}
}
};