Why Memory Limit Exceeded


  • 0
    A

    Can anyone give me some hint why I got Memory Limit Exceeded

    ...

    /* The guess API is defined in the parent class GuessGame.
    @param num, your guess
    @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
    int guess(int num); */

    public class Solution extends GuessGame {
    public int guessNumber(int n) {
    int[] numbers = new int[n];
    for (int i = 0; i < n; i++)
    {
    numbers[i] = i + 1;
    }

        int toReturn = searchNumber(numbers, 0, numbers.length - 1);
        
        return toReturn;
    }
    
    public int searchNumber(int[] numbers, int low, int high)
    {
        if (low > high)
        {
            return -1;
        }
        
        if (numbers.length == 1)
            {
                if (guess(numbers[0]) == 0)
                {
                    return numbers[0];
                }
                else
                {
                    return -1;
                }
            }
            
        int mid = low + (high - low) / 2;
        
        if(guess(numbers[mid]) == 0)
        {
            return numbers[mid];
        }
        
        else if(guess(numbers[mid]) == -1)
        {
            return searchNumber(numbers, low, mid);
        }
        
        else if (guess(numbers[mid]) == 1)
        {
            return searchNumber(numbers, mid + 1, high);
        }
        
        return -1;
    }
    

    }
    ...


  • 1
    J

    Type int is not big enough to hold "low+high". You may need long to receive the number.


Log in to reply
 

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