What is wrong with my code?

  • 0

    I get wrong answer when my code run with test case


    My answer is 1.00000, but the expected answer is 0.00000. Could I have your help to figure out the reason?

    class Solution {
        public double myPow(double x, int n) {
            if(x==0) return 0;
            if(n==0) return 1;
            if(n>0) return helper(x, n);
            else return 1/helper(x,-n);
        public double helper(double x, int n){
            if(n==0) return 1;
            double half = helper(x, n/2);
            if(n%2==1) return half*half*x;
            return half*half;

Log in to reply

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