Simple Log(n) Solution


  • 0
    public class Solution {
        public double MyPow(double x, int n) {
            return Pow(n < 0 ? 1/x : x, n);
        }
        
        public double Pow(double x, int n) {
            if(n==0) return 1;
            else if(n==1 || n==-1) return x;
            double half = Pow(x, n/2);
            return half * half * (n%2==0 ? 1 : x);
        }
    }

Log in to reply
 

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