Skip to content

Commit ca404aa

Browse files
committedJul 24, 2023
leetcode
1 parent b66ea1c commit ca404aa

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed
 

‎PowOfXN/pow_of_x_n.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func myPow(x float64, n int) float64 {
6161
}
6262
6363
64-
*/
64+
6565
6666
6767
@@ -91,3 +91,38 @@ func myPow(x float64, n int) float64 {
9191
9292
return ans
9393
}
94+
*/
95+
96+
// Fastest Bitwise
97+
98+
func power(x float64, n int) float64 {
99+
if n == 0 {
100+
return 1
101+
}
102+
103+
if n%2 == 0 {
104+
return power(x*x, n/2)
105+
}
106+
107+
return x * power(x, n-1)
108+
}
109+
110+
func myPow(x float64, n int) float64 {
111+
if n == 0 {
112+
return 1
113+
}
114+
115+
var l int
116+
if n < 0 {
117+
l = -n
118+
} else {
119+
l = n
120+
}
121+
122+
ans := power(x, l)
123+
if n < 0 {
124+
return 1 / ans
125+
}
126+
127+
return ans
128+
}

0 commit comments

Comments
 (0)
Please sign in to comment.