50. Pow(x, n)

🟧 Medium

Solution

My Solution

func myPow(x float64, n int) float64 {
    minus := n < 0
    if minus{
        n = -n
    }
    var a float64 = 1
    for n !=0 {
        if n & 1 != 0{
            a *= x
        }
        x *= x
        n = n >> 1
    }
    if minus {
        a = 1/a
    }
    return a
}
result

Leetcode: link

Last updated

Was this helpful?