Time Limit Exceeded


  • -1
    S

    // Forward declaration of isBadVersion API.
    bool isBadVersion(int version);

    class Solution {
    public:
    int firstBadVersion(int n) {
    int top=n,low=1,i;
    while(true) {
    if(low>=top) break;
    i = (top+low)>>1;
    if(isBadVersion(i)) top=i;
    else low = i+1;
    }
    return low;
    }
    };


  • 1
    L
     i = (top+low)>>1;
    

    int sum would overflow and leads your bits behave abnormal.


Log in to reply
 

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