Divide-and-Conquer Java


  • 0
    M

    Divide-and-Conquer with helper function. Big-Oh O(log n).

    public class Solution {
        public double myPow(double x, int n) {
            return (n < 0) ? 1 / power(x, n): power(x, n);
        }
        
        public double power(double x, int n) {
            if (n == 0) return 1;
            double result = (power(x, n/2));
            return (n % 2 == 0) ? result * result: 
            x * result * result;
        }
    }

Log in to reply
 

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