StackOverflowError,,,,,,I am confused with problem and don't what is wrong with my code. Wish someone help me.


  • -1
    Z

    public class Solution {
    public double myPow(double x, int n) {
    if(n==1)
    return x;
    if(n==0)
    return 1;
    if(n>0)
    {
    double t=myPow(x,n/2);
    if(n%2==0)
    return tt;
    if(n%2==1)
    return x
    tt;
    }
    else
    return 1.0/myPow(x,-1
    n);
    return -1;
    }
    }


  • 1
    T

    From that mess you have up there it's hard to decipher what's going wrong, I didn't see anything obvious. The problem is that * operator is interpreted as HTML <i> and hence not visible, one can guess, but it's easier if you edit and indent each line by 4 spaces.

    Regardless, you must know an input that fails, either from "Wrong answer" or when you're running the code yourself. Set that input as custom input and add a System.out.printf("%.5f ^ %d\n", x, n) at the beginning of the method to see all your recursive calls.

    Make sure you limit the number of method calls with an instance variable to a reasonable number like 100 to prevent your browser from hanging when receiving the output.


Log in to reply
 

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