Swift solution - Binary Search


  • 0
    class Solution {
        func myPow(_ x: Double, _ n: Int) -> Double {
            if n == 0 {
                return 1
            }
            
            var x = x
            var n = n
            
            if n < 0 {
                n = -n
                x = 1 / x
            }
            
            return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.