Something is broken, it always return Internal Error...


  • 0
    S

    Even I just write the function as "return 0;".....


  • 0
    G

    you need to see if the num.size()<1


  • 0
    S

    I have checked that, but this problem is still there. following is my code:

    int findMin(vector<int> &num) {
    int n = num.size();
    if (n == 0)
    return 0;

        int i = 0;
        int j = n - 1;
        while (i < j - 1)
        {
            int lMid = (i + j)/2;
            if (num[lMid] < num[i])
                j = lMid;
            else if (num[i] < num[lMid])
            {
                if (num[i] < num[j])
                    return num[i];
                else
                    i = lMid;
            }
            else
            {
                if (num[i] < num[j])
                    j--;
                else
                    i++;
            }
        }
        
        if (i == j - 1)
            return num[i] < num[j] ? num[i] : num[j];
        else
            return num[i];
    }

  • 0
    G

    Check this code works !!!

    int findMin(vector<int> &num) { int n = num.size(); if (n <1) return 0;

    int i = 0;
    int j = n - 1;
    while (i < j - 1)
    {
        int lMid = (i + j)/2;
        if (num[lMid] < num[i])
            j = lMid;
        else if (num[i] < num[lMid])
        {
            if (num[i] < num[j])
                return num[i];
            else
                i = lMid;
        }
        else
        {
            if (num[i] < num[j])
                j--;
            else
                i++;
        }
    }
    
    if (i == j - 1)
        return num[i] < num[j] ? num[i] : num[j];
    else
        return num[i];
    

    }


Log in to reply
 

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