Swift - with O(Log(n)) complexity, 100 % Test Pass


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

Log in to reply
 

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