What is wrong with my code?


  • 0
    H

    I get wrong answer when my code run with test case

    2.00000
    -2147483648
    

    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.