One minute
Leetcode 50
This solution is if n < 0 (If n is negative) we can return 1 / (x ^ (-n)) (The -n is for making n positive). Otherwise we can just return math.Pow(x, n).
func myPow(x float64, n int) float64 {
if n < 0 {
return 1 / math.Pow(x, float64(-n))
}
return math.Pow(x, float64(n))
}
Technically we could do the following code and solve the problem, but that isn’t the idea of the problem because Golang takes care of test cases when n is negative.
func myPow(x float64, n int) float64 {
return math.Pow(x, float64(n))
}
Read other posts